In this post, we will learn how to count the number of digits in a number using the C++ Programming language.
This program asks the user to enter a number, then it calculates the total no. of digits available in that number using the following approaches:
- Using For Loop
- Using While Loop
- Using Functions
So, without further ado, let’s begin this tutorial.
C++ Program to Count Number of Digits in a Number Using For Loop
// C++ Program to Count Number of Digits in a Number #include <iostream> using namespace std; int main(){ int num, count; // Asking for input cout << "Enter a number: "; cin >> num; // Counting no. of digits for (count = 0; num > 0; count++){ num = num / 10; } // Displaying output cout << "Total no. of digits: " << count << endl; return 0; }
Output
Enter a number: 15638
Total no. of digits: 5
How Does This Program Work?
int num, count;
In this program, we have declared two integer data type variables named num and count.
// Asking for input cout << "Enter a number: "; cin >> num;
The user is asked to enter a number. This number gets stored in the num named variable.
// Counting no. of digits for (count = 0; num > 0; count++){ num = num / 10; }
We have used for loop to count the total no. of digits.
Suppose the user enters a number 527, then:
- 1st For Loop Iteration: for (count = 0; 527 > 0; count++)
- num = 527 / 10
- Here the condition is True, so the loop will execute. Then count becomes 1 and num becomes 52.
- 2nd For Loop Iteration: for ( count = 1; 52 > 0; count++)
- num = 52 / 10
- Here the condition is True, so the loop will execute. Then count becomes 2 and num becomes 5.
- 3rd For Loop Iteration: for (count = 2; 5 > 0; count++)
- num = 5 / 10
- Here also, the condition is True, so the loop will execute and count becomes 3 and num becomes 0.
- 4th For Loop Iteration: for (count = 3; 0 > 0; count++)
- Here, the condition is False, so the loop will terminate.
We get count = 3, which is the total number of digits in the entered number.
C++ Program to Count Number of Digits in a Number Using While Loop
// C++ Program to Count Number of Digits in a Number #include <iostream> using namespace std; int main(){ int num, count = 0; // Asking for input cout << "Enter a number: "; cin >> num; // Counting total number of digits while (num > 0){ num = num / 10; count++; } // Displaying output cout << "Total no. of digits: " << count << endl; return 0; }
Output
Enter a number: 1234
Total no. of digits: 4
C++ Program to Count Number of Digits in a Number Using Functions
// C++ Program to Count Number of Digits in a Number Using Functions #include <iostream> using namespace std; int countDigits(int); int main(){ int num, count; // Asking for input cout << "Enter a number: "; cin >> num; // Calling out custom function count = countDigits(num); // Displaying output cout << "Total no. of digits: " << count << endl; return 0; } int countDigits(int n){ int total = 0; while (n > 0){ n = n / 10; total++; } return total; }
Output
Enter a number: 1234567
Total no. of digits: 7
Conclusion
I hope after going through this post, you understand how to count the number of digits in a number using C++ Programming language.
If you have any doubts regarding the program, then contact us in the comment section. We will be delighted to assist you.
Also Read: