Program to find the length of a number in C++ (CPP)
C++ Program to Find the Number of Digits from any number
is today’s topic of discussion in this tutorial.
Flowchart of C++ Program to Find the Number of Digits of a number
Example: When the total digits a number are 1
Example: When the total digits a number are 2
C++ Program to Find the Number of Digits Using while loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<iostream> using namespace std; int main() { int num; int a=0; cout<<"Please Enter any number : "; cin>>num; while(num>0) { num=num/10; // a++; } cout<<"Number of digits in given number is: "<<a; } |
Output
Please Enter any number : 4
Number of digits in given number is: 1
——————————–
With Comments – Program to find the length of a number in C++ (CPP)
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 28 29 30 31 | #include<iostream> //adding the header file using namespace std; //adding the namespace int main() //main function { //start of main int num; // integer type variable num declaration int a=0; // integer type variable a declaration and initialziation with 0 cout<<"Please Enter any number : "; //display message same to same cin>>num; //take value of variable num from the user. while(num>0) //loop { num=num/10; //divide the number with 10, and the result will be 5698/10 = 570 //divide the 570 with 10, and the result will be 570/10 = 57 //divide the 57 with 10, and the result will be 57/10 = 6 //divide the 6 with 10. and the result will be 6/10 = 0 a++; //increment the loop } //ending of loop cout<<"Number of digits in given number is: "<<a; //display message and print the variable of variable a }//ending of main function |
Output:
Program of Length of a Number in C++ Using Do While Loop (only for positive values)
Let’s begin with “Program of Length of a Number in C++ Using Do While Loop only for positive values”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<iostream> using namespace std; int main() { int num; int i; cout<<"Please enter a number to find its length"<<endl; cin>>num; do { num=num/10; i++; } while(num>0); cout<<"the length of the number is = "<<i<<endl; system("pause"); } |
Output
Please enter a number to find its length
545
the length of the number is = 3
Program of Length of a Number in C++ Using For Loop (only for positive values)
Let’s begin with “Program of Length of a Number in C++ Using For Loop only for positive values”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<iostream> using namespace std; int main() { int num; int i ; cout<<"Please enter a number to find its length"<<endl; cin>>num; for(;num>0; i++) { num=num/10; } cout<<"the length of the number is = "<<i<<endl; system("pause"); } |
Output
Program of Length of a Number in C++ Using Functions
Let’s begin with “Program of Length of a Number in C++ Using Functions”.
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 28 | #include<iostream> using namespace std; int length(int,int&); int main() { int n; int p; cout<<"Please enter a number to find its length"<<endl; cin>>n; length(n,p); cout<<"the length of the number is = "<<p<<endl; system("pause"); } int length(int num, int &i) { i=0; do { num=num/10; i++; } while(num>0); } |
Program of Length of a Number in C++ Using Nested if Statement
Let’s begin with “Program of Length of a Number in C++ Using Nested If Statement”.
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 28 29 30 31 32 33 34 35 36 37 | #include<iostream> using namespace std; int main() { int num; int i; for(int g=0; g<1;) { cout<<"Please enter a number to find its length"<<endl; cin>>num; if(num!=0) { for(int p=0; p<1;) { if(num!=0) { num=num/10; i++; } else { p++; } } g=1; cout<<"the length of the number is = "<<i<<endl; system("pause"); } else { cout<<"Please enter non-zero integers.."<<endl; } } } |
Program of Length of a Number in C++ Using for loop and if Statement
Let’s begin with “Program of Length of a Number in C++ Using for loop and If Statement”.
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 | #include<iostream> using namespace std; int main() { int num; int i; cout<<"Please enter a number to find its length"<<endl; cin>>num; for(int p=0; p<1;) { if(num!=0) { num=num/10; i++; } else { p++; } } cout<<"the length of the number is = "<<i<<endl; system("pause"); } |
Program of Length of a Number in C++ Using While Loop
Let’s begin with “Program of Length of a Number in C++ Using while loop”.
Program to find the length of a number in C
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 28 29 30 31 | #include<iostream> using namespace std; int main() { int num; int i; cout<<"Please enter a number to find its length"<<endl; cin>>num; for(;num>0; i++) { num=num/10; } cout<<"the length of the number is = "<<i<<endl; system("pause"); } #include<stdio.h> int main() { int num; int a=0; printf("Please Enter any number : "); scanf("%d" , &num); while(num>0) { num=num/10; a++; } printf("Number of digits in given number is: %d", a); } |
With Comments – Program to find the length of a number in C++ (CPP)
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 28 29 30 31 | #include<stdio.h> //adding the header file int main() //main function { //start of main function int num; // declaration of integer type variable num int a=0; // declaration of integer type variable a and initializing the a with 0 printf("Please Enter any number : "); //display the message scanf("%d" , &num); //takes the integer type value from the user as input while(num>0) // loop { //start of loop num=num/10; //divide the number with 10, and the result will be 5698/10 = 570 //divide the 570 with 10, and the result will be 570/10 = 57 //divide the 57 with 10, and the result will be 57/10 = 6 //divide the 6 with 10. and the result will be 6/10 = 0 a++; //increment the loop } //ending of loop printf("Number of digits in given number is: %d", a); //print the message with value of a } //ending the main function |