length of number in C++ by R4R Team

Example-
Number is 3245
Length is: 4.
Number is 9087245
Length is: 7


program:

#include < iostream>
using namespace std;
class r4r
{
public:
int func(int n)
{
int count=0;
while(n)
{
n=n/10;
count++;
}
return count;
}
};
int main()
{
int n;
r4r obj;
cout<<"Enter any number"<< endl;
cin>>n;
cout<<"Length is: "<< obj.func(n);
return 0;
}

output-

Enter number
34400
Length is: 5

-In this program, we take a input of the integer number, and for each digit we run the while loop and increase the value of count by (count++) and after loop we have a number of digits in count variable so we return that.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!