cdigitsremainder

How to find first two digits in C


Hello I have code to find out the first two digits of a number and save them to a variable in C and the code only works for even length numbers. I need something that will work for both even and odd length numbers. The number I need for first2 is the first two numbers.

        long long int input = 6789466321
        first2 = input;
        while(first2 >= 100)
        {
            first2 = first2 / 100;
        }

Solution

  • Divide by 10 at a time instead of 100 so that you are only removing one digit each iteration.