C++ program to find number of digits in a number. - coderforcoders

Search

Wednesday, 13 March 2019

C++ program to find number of digits in a number.

#include<iostream>
using namespace std;
class numberOfDigits{
public:
int num,count=0,temp;
void countDigit()
{
temp=num;
while(temp)
{
temp=temp/10;
count++;
}
cout<<"There are "<<count<<" digits in the number "<<num<<endl;
}
};
int main()
{
numberOfDigits obj1;
cout<<"please enter a number\n";
cin>>obj1.num;
obj1.countDigit();
cout<<endl;
}


Output 1:
Please enter a number
1
There are 1 digits in the number 1

Output 2:
Please enter a number
345
There are 3 digits in the number 345

Output 3:
Please enter a number
123456
There are 6 digits in the number 123456



No comments:

Post a Comment