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



Write a program that will count the number of digits in an input integer up to value MAX_VALUE (214..

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

Write a program that will count the number of digits in an input integer up to value MAX_VALUE (214..

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

Post New Answer

More C++ General Interview Questions

can any one help to find a specific string between html tags which is changed to a sting.. weather.html looks (for location) is <location>somewhere</location> #include <iostream> #include <fstream> #include <string> using namespace std; string find_field(string myPage,string); int main (void) { string page, line, location, temperature; ifstream inputFile("weather.xml"); while(getline(inputFile, line)) { page.append(line); line.erase(); } // Now page is a string that contains the whole xml page // Here you need to write something that finds and // extracts location and temperature from the XML // data in the string page and stores them in // the strings location and temperature respectively location=find_field(page,"location"); temperature=find_field(page,"temp_c"); cout << "Location: "<<location << endl; cout << "Temperature: " << temperature << endl; system("pause"); } string find_field(string myPage,string find_string){ int temp=myPage.find(find_string); if(temp!=string::npos) { cout << "Match found at " << temp << endl; } return "found?"; } ///

0 Answers  


Does c++ have foreach?

0 Answers  


Can there be at least some solution to determine the number of arguments passed to a variable argument list function?

0 Answers  


How do you test your code?

4 Answers   Microsoft,


Is c++ a float?

0 Answers  






What are the differences between a struct in C and in C++?

8 Answers   Amazon, Wipro,


Which operations are permitted on pointers?

0 Answers  


Explain the problem with overriding functions

0 Answers  


What is a volatile variable in c++?

0 Answers  


Will a C compiler always compile C++ code a) Yes b) No c) Only optimized compilers

0 Answers  


Explain the isa and hasa class relationships. How would you implement each?

0 Answers  


How do you traverse a btree in backward in-order?

0 Answers  


Categories