Count Digits of Number C Program

By | September 17, 2015

C Program To Count Digits of Number

Learn How To Count Digits of Number in C Programming Language. It is important that we should know How A For Loop Works before getting further with the C Program Code.

Method 1: C Program To Count Digits of an Integer using While Loop

#include<stdio.h>

int main()
{
    int num1, count = 0, rem;
    printf("\nEnter a Number:\t");
    scanf("%d", &num1);
    while(num1 != 0)
    {
        num1 = num1/10;
        count++;
    }
    printf("\nTotal Count:\t%d\n", count);
    return 0;
}

Method 2: Count Number of Digits in a Number in C Programming using For Loop

#include<stdio.h>

int main()
{
    int num1, count, rem;
    printf("\nEnter a Number:\t");
    scanf("%d", &num1);
    for(count = 0; num1 != 0; count++;)
    {
        num1 = num1/10;
    }
    printf("\nTotal Count:\t%d\n", count);
    return 0;
}

Method 3: C Program To Count Digits of a Number using Function

#include<stdio.h>

int count(int x);

int main()
{
    int res, num;
    printf("\nEnter a Number:\t");
    scanf("%d", &num);
    res = count(num);
    printf("\nNumber of Digits : \t%d\n", res);
    return 0;
}

int count(int x)
{
    int total = 0, i;
    if(x == 0)
    {
        return total;
    }
    else
    {
        for(i = 0;x != 0;i++)
        {
            x = x/10;
            total++;
        }
        return total;
    }
}

Output

Code Count Digits of Number in C Programming Language

In case you get any Compilation Errors with this Program To Count Digits of Number in C Programming Language or you have any doubt about it, let us know about it in the Comment Section below.

2 thoughts on “Count Digits of Number C Program

  1. Sachin Bhoi

    Please help me to understand the difference between Modulus Operator and Division Operator in this C Prpgram to find the number of digits in the number.

    Reply
  2. Parag Vidhate

    This means that we just have to remove the digits in a number one by one and simultaneously count the removals till the number is not equal to zero.

    Reply

Let's Discuss