Write a program to read an integer number and find its number of digits ( for ex
ID: 3628814 • Letter: W
Question
Write a program to read an integer number and find its number of digits ( for example, 3459 has 4 digits and 125860 has 6 digits).The program should be designed in a way that the user could test as many numbers as desired without leaving the program. Use only long integer ( int or long int ) data type in this program; however make sure that the program rejects any input outside the long integer data range ( about -2000000000 to +2000000000).
The output of the program should be written to an output file having a format exactly like the following table.
VALUE ENTERED NUMBER OF DIGITS
1241 4
19 2
5684900 7
0 1
-153 3
4000000000 Invalid
Test the program for at least seven integer values between 2,000,000,000 and a number outside the long int data range.
Maximum value for long int is 2147483647.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
using namespace std;
int main()
{long num;
int count;
ofstream out;
out.open("output.txt");
out<<"VALUE ENTERED NUMBER OF DIGITS ";
cout<<"Enter a number (CTRL^Z to exit): ";
cin>>num;
while(cin)
{count=0;
out<<num<<" ";
{ if(num<0)
num*=-1;
if(num==0)
count=1;
while(num>0)
{count++;
num/=10;
}
out<<count<<endl;
}
cout<<"Enter a number (CTRL^Z to exit): ";
cin>>num;
while(cin.fail())
{if(cin.eof())
{out.close();
return 0;
}
out<<num<<" ";
cin.clear();
out<<"invalid ";
cout<<"Enter a number (CTRL^Z to exit): ";
cin>>num;
}
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.