Code in Visual Studio C++ Write a program to read an integer number and find its
ID: 3888354 • Letter: C
Question
Code in Visual Studio C++
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 problem: however make sure that the program The outputs of the program should be written to an output file having a format similar to the following table. NUMBER OF DIGITS 1241 19 5684900 Al 1267678901 -153 62000 Test the program for at least seven integer values between ± 2.000.000.000 and a number outside the long int data range. Remember to submit a copy of the program file and a copy of the output file. Note: You should use while or do-while loop in this assignment.Explanation / Answer
#include<iostream>
#include <fstream>
using namespace std;
//Function to count number of digits and store it in file
void countNumberOfDigits(long int numbers[], int no)
{
//Creates an object of ofstream class
ofstream fw;
//Open the specified file for writing
fw.open ("Result.txt");
//Counter is initialized to zero
int counter = 0;
//To store number of digits and the number
int numberOfDigit, number;
//Write the heading to the file
fw<<" Number Digits ";
//Loops till the total numbers entered
while(counter < no)
{
//Initializes the number of digits to zero for each number
numberOfDigit = 0;
//Creates a duplicate copy of the existing number
number = numbers[counter];
//Loops till the number becomes zero
do
{
//Increase the number of digit counter by one
numberOfDigit++;
//Divide the number by ten and stores it in the number
number /= 10;
}while((number) != 0);//End of do - while loop
//Write the number and number of digits to file
fw<<endl<<numbers[counter]<<" "<<numberOfDigit;
//Increase the counter by one
counter++;
}//End of while
}//End of function
//Main function definition
int main()
{
//Declares an array to store numbers
long int numbers[100];
int counter = 0, no;
//Accept the total number count
cout<<" How many numbers you want? ";
cin>>no;
//Loops till counter is less than the total numbers value
while(counter < no)
{
//Accept the number
cout<<" Enter number "<<counter + 1<<" : ";
cin>>numbers[counter++];
}//End of while loop
//Calls the function to count number of digit of each number and store it in the file
countNumberOfDigits(numbers, no);
}//End of main function
File Result.txt contents
Number Digits
1241 4
19 2
5684900 7
0 1
1267678901 10
-153 3
62000 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.