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

- 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 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
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...
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
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...
(8 marks) Write a program to ask user to enter an integer that represents the number...
Write a program to ask user to enter an integer that represents the number of elements, then generate an ArrayList containing elements which are all random integers in range [75, 144] , and finally display index and value of each element. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use ArrayList. Your program must use only printf(…) statements to adjust the alignment of your output. Your code must display the index in descending...
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT