Write a program that will count the number of digits in an
input integer up to value MAX_VALUE (2147483647). Thus, for
an input of 5837 the output should be
4 digits
Make sure that your program works for the numbers 0, 1, and
10. For the number 0, the output should be
1 digit
Answers were Sorted based on User's Feedback
Answer / usama azam
#include<iostream.h>
int main ()
{
int num;
cout<<"enter the number ";
cin>>num;
if (num>=0 && num<=9)
cout<<"You have entered one digit";
else if (num>=10 && num<=99)
cout<<"You have entered two digits";
else if (num>=100 && num<=999)
cout<<"You have entered three digits";
else if (num>=1000 && num<=9999)
cout<<"You have entered four digits";
else if (num>=10000 && num<=99999)
cout<<"You have entered five digits";
else if (num>=100000 && num<=999999)
cout<<"You have entered six digits";
else if (num>=1000000 || num<=9999999)
cout<<"You have entered seven digits";
system("pause");
return 0;
}
Is This Answer Correct ? | 6 Yes | 2 No |
Answer / sandeep mannarakkal
I have another suggestion for the above,
int iInputNumber;// This number is used for collecting input from user.
int nCount = 1;
while ( iInputNumber /10 )
{
iInputNumber = iInputNumber/10;
nCount ++;
}
cout << nCount << endl; // nCount will have the count of digits.
Is This Answer Correct ? | 0 Yes | 0 No |
What is the array and initializing arrays in c++?
Why is c++ still popular?
Write syntax to define friend functions in C++.
Generally variables are stored in heap memory. When he variables are created in stack?
What should main() return in c and c++?
Given the following seqment of code containing a group of nested if instructions: y = 9; if ((x==3) || (x == 5)) y++; else if (x == 2) y *= 2; else if (x == ) y-= 7; else y = 8; if the value of x is 4 before the nested IFs are executed, what is the value of y after the nested IFs are executed?
Define friend function.
What are multiple inheritances (virtual inheritance)? What are its advantages and disadvantages?
Why is polymorphism useful?
How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array?how can you find the nodes with repetetive data in a linked list?
What is a dangling pointer in c++?
What is name mangling?