In this tutorial you will learn about the C++ Programs to Find Number Of Digits and its application with practical example.
C++ Program to Find the number of digits.
In this tutorial, we will learn to create a C++ program that will Find the Number Of Digits in C++ programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators in C++ Programming.
- Basic Input and Output function in C++ Programming.
- Basic C++ programming.
- String functions in c++ programming.
Find the number of digits in the number.
As we all know the c++ programming is a very powerful language. In the c++ programming language, we have many pre-defined functions for arithmetic operations. but today we will count the number of digits in a number without using the mod operator.
With the help of this program, we can find the number of digits in numbers.
Algorithm Find the Number Of Digits:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the variables required for the program. 2. Taking the input number from the user for the program. 3. Calculating the total number of digits in the numbers. 4. Calculating the sum of all the numbers in the given number. 5. Printing the result numbers. 6. End Program |
Program to Find the Number Of Digits:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// W3 Adda C++ Tutorial // c++ program to find number of digits in a number #include <bits/stdc++.h> using namespace std; int flagDigit(long long n) { //Declaring the variables required for the function int flag = 0; while (n != 0) { n = n / 10; ++flag; } return flag; } // Driver code int main(void) { //Declaring the variables required for the program //Defining the value for the variable long long n = 345289467; //Printing output for the program cout << "Number of digits : " << flagDigit(n); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- n = it will hold the long integer.
- flag = it will hold the integer value for numbers.
Taking input number to the user.
Calling function for reverse and printing
Finding the number of digits with the help of using a user-defined function.