Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Program to find the length of a number in c and cplusplus (cpp)

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;

  1. Divide the number with 10, and the result will be 5698/10 = 570
  2. Divide the 570 with 10, and the result will be 570/10 = 57
  3. Divide the 57 with 10, and the result will be 57/10 = 6
  4. 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

length of a number in c and cplusplus (cpp)

          Figure: 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:

length of a number in c output

Figure: length of a number.

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

 

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.

Leave a Reply

Your email address will not be published. Required fields are marked *