Question

In: Computer Science

Write a C++ program that keeps asking user to enter a positive integer number until a...

Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered.

Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.)

An example of executing such a program is shown below. Note that the user input is in italic font.

Enter 999 to end this program.
Please enter a positive integer number: 1984
There are 4 digits in the number you just entered.

Enter 999 to end this program.
Please enter a positive integer number: -98
It must be a positive value.
Please enter a positive integer number: -8
It must be a positive value.
Please enter a positive integer number: 0
It must be a positive value.
Please enter a positive integer number: 6856298
There are 7 digits in the number you just entered.

Enter 999 to end this program.
Please enter a positive integer number: -9
It must be a positive value.
Please enter a positive integer number: 999
Bye!

In this program, you can safely assume that user will always enter the integer values for input.

Solutions

Expert Solution

C++ code:

#include <iostream>
using namespace std;
int main()
{
    //initializing nuumber and count
    int num,count;
    //asking to enter 999 to end program
    cout<<"Enter 999 to end this program."<<endl;
    //asking for positive integer
    cout<<"Please enter a positive integer number: ";
    //accepting it
    cin>>num;
    //looping till user enter 999
    while(num!=999){
        //checking if the number is less than or equal to 0
        if(num<=0)
            //printing it must be positive value
            cout<<"It must be a positive value."<<endl;
        else{
            //initializing count as 0
            count=0;
            //looping till number is 0
            while(num!=0){
                //incrementing count
                count++;
                //dividing number by 10
                num/=10;
            }
            //printing number of digits
            cout<<"There are "<<count<<" digits in the number you just entered."<<endl;
            //asking to enter 999 to end program
            cout<<"\nEnter 999 to end this program."<<endl;
        }
        //asking for positive integer
        cout<<"Please enter a positive integer number: ";
        //accepting it
        cin>>num;
    }
    //printing Bye
    cout<<"Bye!"<<endl;
    return 0;
}


Screenshot:


Input and Output:


Related Solutions

In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
- Write a function with no input parameter which keeps asking the user to enter positive...
- Write a function with no input parameter which keeps asking the user to enter positive numbers until the user enters an invalid input. (An invalid input is an input which includes at least one alphabet, like 123d4). The program should print the Max and Min of the numbers the user had entered as well as the distance between the Max and Min. (Remember to calculate the absolute distance). The function does not return anything
1. Write a program that keeps asking the user for a password until they correctly name...
1. Write a program that keeps asking the user for a password until they correctly name it. Once they correctly enter the password, the program congratulates the user and tells them how many guesses it took. Be sure to be grammatically correct with the guess/guesses output. Call the program LastNamePassword. Example (user input in italics) What is the password? monkeys Incorrect. Guess again. dishwasher Incorrect. Guess again. aardvark Correct! You got the password, and it took you 3 guesses to...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
First, write a program to loop asking for a number from the user until the user...
First, write a program to loop asking for a number from the user until the user inputs a zero or until the user has input 50 numbers. Store each value in an array except the values that are divisible by 5 and display all stored values. Determine how many of the values stored in the array were divisible by 10 and display result. Next, write a function getMinMax that accepts an array of floats and the size of the array,...
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT