Count of digits
Sara is studying in 2nd standard and she has to play event in her school. The number is displayed on the screen and she has to count the number of digits in the displayed number. Can you help her by writing the program for the above scenario?
Input Format:
The input consist of an integer
Output Format:
Print the number of digits in the given number
Sample Input:
1234
Sample Output:
4
-------------------------------------------------------------------------------------------------------------
My Program is:
#include
using namespace std;
int main()
{
int n,i;
cin>>n;
while(n>0)
{
n=n/10;
i++;
}
cout<
}
-------------------------------------------------------------------------------------------------
Problem:
I was getting 1 test case failed and it's a hidden test case.
I think that the input will start with 0 so while reading it ,it vanishes.So plz help me in sloving this
Answers
Answered by
17
Answer:
We have to calculate for not only greater than ZERO ,but also we need to calculate for ZERO also.If input is ZERO,then we have to print the output as 1.
Explanation:
#include<iostream>
int main()
{
int n,i=0;
std::cin>>n;
if(n==0)
{
n=1;
}
while(n>0)
{
n/=10; //(n=n/10)
i++;
}
std::cout<<i;
return 0;
}
Answered by
18
Answer:#include<iostream>
int main()
{
// Type your code here
int n,i;
i=0;
std::cin>>n;
do{
n=n/10;
i++;
}while(n>0);
std::cout<<i;
}
Explanation:
Similar questions
Political Science,
5 months ago
English,
5 months ago
Chemistry,
10 months ago
Biology,
1 year ago
Math,
1 year ago