Question

C++ Write a program that asks user to input three positive integers one at a time,...

C++

Write a program that asks user to input three positive integers one at a time, then compute and output the largest entered number. Use if-else statements for three integer comparison and use for loops for a user validation.

Example output:

Enter an integer: -7

Invalid! Number must be positive.

Enter an integer: -2

Invalid! Number must be positive.

Enter an integer: 5

Enter an integer: 7

Enter an integer: 1

Largest number: 7

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For loops are used or all the variables separately and they will run till user enters positive value of the variables.

After all the input are valid then using if else statements the largest of three numbers are found out and printed.

Editable Code:

# include <iostream>

using namespace std;

int main(){
   int a, b, c, i; //Declaring variables
   cout << "Enter an integer. ";
   cin >> a;
   for(i=0;a<=0;i++){ //Run loop till user enters positive value of a
       cout <<"Invalid! Number must be positive.\n";
       cout << "Enter an integer. ";
       cin >> a;  
   }
   cout << "Enter an integer. ";
   cin >> b;
   for(i=0;b<=0;i++){ //Run loop till user enters positive value of b
       cout <<"Invalid! Number must be positive.\n";
       cout << "Enter an integer. ";
       cin >> b;  
   }
   cout << "Enter an integer. ";
   cin >> c;
   for(i=0;c<=0;i++){ //Run loop till user enters positive value of c
       cout <<"Invalid! Number must be positive.\n";
       cout << "Enter an integer. ";
       cin >> c;  
   }
  
   //Finding the largest
   if(a>=c && a>=b){       //If a is the largest
       cout << "Largest number: "<<a;
   }
   else if (b>=a && b >= c ){//If b is the largest
       cout <<"Largest Number: "<<b;  
   }
   else {                   //If c is the largest
       cout<<"Largesr Number: "<<c;
   }
      
return 0;
}

Hope this helps
If you have any doubt feel free to comment
Thank You!!

Add a comment
Know the answer?
Add Answer to:
C++ Write a program that asks user to input three positive integers one at a time,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a program that reads three positive integers (> 0) from the command line (one...

    C++ Write a program that reads three positive integers (> 0) from the command line (one at a time), then computes and prints the smallest entered number. Use if-else statements for three integer comparison. Example: Enter an integer: 5 Enter an integer: 23 Enter an integer: 7 The smallest number is: 5 Example 2: Enter an integer: 3 Enter an integer: 3 Enter an integer: 6 The smallest number is: 3 "The input must be positive number. The program should...

  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 5. Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. Input Validation: Do not accept a negative starting number.

  • Write a python program that repeatedly asks the user to input a pair of integers. The...

    Write a python program that repeatedly asks the user to input a pair of integers. The program should record the largest number of each pair into a list. The program should keep asking the user to input pairs of numbers until the user enters -1 for the first number. At this point the program should print the list on a single line, with each value separated by a space and then stop. Example of expected behaviour: Please enter first number:...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Write a C++ program that repeatedly collects positive integers from the user, stopping when the user...

    Write a C++ program that repeatedly collects positive integers from the user, stopping when the user enters a negative number or zero. After that, output the largest positive number entered. A sample run should appear on the screen like the text below. Enter a number: 3 Enter a number: 10 Enter a number: 2 Enter a number: -213 Output: The largest positive number you entered was 10.

  • In Python, write a program that asks the user for a positive integer number. If the...

    In Python, write a program that asks the user for a positive integer number. If the user enters anything else, the program would ask the user to re-enter a number or enter -1 to quit. If it is a positive number, the program then prints out a multiplication table that goes up to that number. For example, if the user enters 10, the output would look something like this. https://www.vectorstock.com/royalty-free-vector/multiplication-table-on-white-background-vector-2721686

  • Write a program that asks the user how many integers they would like to enter. You...

    Write a program that asks the user how many integers they would like to enter. You can assume they will enter an integer >= 1. The program will then prompt the user to enter that many integers. After all the numbers have been entered, the program should display the largest and smallest of those numbers (no, you cannot use lists, or any other material we haven't covered). When you run your program it should match the following format: How many...

  • Write a program in JAVA that asks the user to enter an integer. Store the integers...

    Write a program in JAVA that asks the user to enter an integer. Store the integers in an array. Allow for up to 10 integers. When the user enters -1, the program should end. Print the number of integers entered as well as the number of times each integer was entered.

  • Write a Java program that: • Asks the user to enter the number of integers that...

    Write a Java program that: • Asks the user to enter the number of integers that he need to enter; • Asked him to enter integer by integer; • Print The number of Odd numbers entered and the number of Even numbers entered. Important notes: 1. You should have to copy and paste the Java as your answer for this question. DON’T take screen shot for your Java Code. It must be editable. 2. Take a screen shot for your...

  • Write a complete C++ program to ask the user to enter 4 integers. The program must...

    Write a complete C++ program to ask the user to enter 4 integers. The program must use a function to find the smallest integer among them and print it on the screen. Typical output screen should be as following: Write a complete C++ program to ask the user to enter 4 integers. The program must use a functionto find the smallest integer among them and print it on the screen. Typical output screen should be as following: Note: the code...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT