Count number of digits

Count Number of Digits

C program to count number of digits in a number

This C program, takes an integer from the user and calculates the number of digits. And then, it will divide the given number into individual digits and count those individual digits using While Loop.

For example: If the user enters 2319, the output of the program will be 4.

#include <stdio.h>
#include<conio.h>
void main() {
    long long n;
    int count = 0;
printf("\n\t\t\t...Welcome To EduNews.Tech... ");

    printf("\n\nEnter an integer : ");
    scanf("%lld", &n);
 
    // iterate until n becomes 0
    // remove last digit from n in each iteration
    // increase count by 1 in each iteration
    while (n != 0) {
        n /= 10;     // n = n/10
        ++count;
    }
    printf("\nNumber of digits : %d", count);
printf("\n\n\n\t\t\tThankyou for Joining Us !");
    printf("\n\t\t\t!Regards EduNews !");

getch();
}

Program Output:

count numbers of digit

I hope this post helps you to understand the C Program to “Count Number of Digits” and its implementation in C programming language.

Keep coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *