Sunday 18 December 2011

Finding the number of digits in a number

Below is the program for finding the number of digits in a number.

#include <stdio.h>

int main(int argc, char *argv[])
{
    int num;
    printf("Enter the number\n");
    scanf("%d", &num);
    int i = 0;
    while(num)                       
    {
        num = num / 10;       //Repetitively divide the number until num is zero. Then, loop will end.
        i++;
    }

    printf("The number of digits in number is %d\n", i);
    return 0;
}

Please feel free to give your suggestions or make some corrections.

No comments:

Post a Comment