Question - given any Integer number, we have to find the number of digit in that number.
Example -
Input : 1254
Output : 4
Input : 12563
Output : 5
Approach -
First we declare a count variable with 0 and divide number and increase count until number is greater than 0.
Implementation -
//To find the no of digit in a given number
#include<iostream>
using namespace std;
int countdigit(int num)
{
int count=0;
while(num){
num/=10;
count++;
}
return count;
}
int main()
{
int num;
cout<<"Enter the Integer number to find the no of digit : ";
cin>>num;
int count=countdigit(num);
cout<<"Number of digit in "<<num<<" is : "<<count<<endl;
return 0;
}
Time complixity - O(n)
Comments
Post a Comment