Number of digit in any integer in C++ by R4R Team

Example-
Number is 3245
Number of digits is 4.
Number is 9087245
Number of digits 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<<"Number of digits is: "<< obj.func(n);
return 0;
}

output-

Enter number
34400
Number of digit in number 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!