Table of Contents

**Program to find the length of a number in c and C plus plus (cpp)**

Suppose the number is 5698, then we can find the length of this number by logic as;

- 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

Congratulations: You successfully find the length of the number. Similarly, you can find the length of any number.

**Flowchart to find the length of a number**

**Program to find the length of a number in C++ (CPP)**

#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; }

**With Comments – Program to find the length of a number in C++ (CPP)**

#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 to find the length of a number in C**

#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)**

#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