C++ Program to Count Total Digits in a Number

This article provides you some programs in C++ that count the total number of digits available in a number entered by user at run-time of the program. This article deals with:

  • Count digits of a number using while loop
  • Count digits of a number using for loop
  • Count digits of a number using user-defined function

For example, if user enters a number say 13204, then the output will be 5. Because there are 5 digits available in the given number.

Count Total Digits in a Number using while Loop

The question is, write a C++ program that receives a number from user to count and print the total number of digits available in that given number. Here is the answer to this question:

#include<iostream>

using namespace std;
int main()
{
   int num, tot=0;
   cout<<"Enter the Number: ";
   cin>>num;
   while(num>0)
   {
      tot++;
      num = num/10;
   }
   cout<<"\nTotal Digits = "<<tot;
   cout<<endl;
   return 0;
}

Here is the initial output produced by above C++ program on counting total number of digits available in a given number by user at run-time:

c++ program count total digits in number

Now enter a number say 43024 as input and press ENTER key to see the output like shown in the snapshot given below:

count total digits in given number c++

The dry run of above program with user input 43024 goes like:

  • When user enters the number, it gets stored in num variable. So num=43024
  • After receiving the number from user, the execution of while loop begins
  • That is, the condition num>0 or 43024>0 evaluates to be True
  • Therefore program flow goes inside the loop
  • The value of tot gets incremented. Because the initial value of tot is 0. Therefore tot=1 now
  • And the statement num = num/10; gets executed
  • Therefore num/10 or 43024/10 or 4302 gets initialized to num. So num=4302
  • Again the condition of while loop gets evaluated
  • That is, the condition num>0 or 4302>0 evaluates to be True
  • Therefore program flow again goes inside the loop
  • The value of tot again gets incremented. So tot=2 now
  • And num/10 or 4302/10 or 430 gets initialized to num. Therefore num=430 now
  • Again third time, the condition of while loop gets evaluated
  • This process continues, until the condition evaluates to be False
  • In this way, after exiting from the loop, or when the condition evaluates to be False. The variable tot holds the value that is equal to the total number of digits available in the number
  • Therefore just print the value of tot as output

Count Digits in a Number using for Loop

This program does the same job as of previous program. The only difference is its approach. That is, this program is created using for loop instead of while as done in previous program.

#include<iostream>

using namespace std;
int main()
{
   int num, tot;
   cout<<"Enter the Number: ";
   cin>>num;
   for(tot=0; num>0; tot++)
      num = num/10;
   cout<<"\nTotal Digits = "<<tot;
   cout<<endl;
   return 0;
}

This program produces same output as of previous program.

Count Digits in a Number using Function

This is the last program on counting the total number of digits available in a number entered by user, using a user-defined function like shown in the program given below:

#include<iostream>

using namespace std;

int myfun(int);
int main()
{
   int num, tot;
   cout<<"Enter the Number: ";
   cin>>num;
   tot = myfun(num);
   cout<<"\nTotal Digits = "<<tot;
   cout<<endl;
   return 0;
}
int myfun(int n)
{
   int t;
   for(t=0; n>0; t++)
      n /= 10;
   return t;
}

C++ Online Test


« Previous Program Next Program »

Follow/Like Us on Facebook




Subscribe Us on YouTube