Question

C++ class homework Topics If/Else statement Description    Write a program that determines the larger of...

C++ class homework

Topics

If/Else statement

Description

  

Write a program that determines the larger of the two numbers provided by the user. The program asks the user to enter a number. Then it asks the user to enter another but a different number. Then, it determines the larger of the two numbers and displays it to the user. (If the user happens to enter the two numbers the same, the program may report either of the two numbers as larger.)

Requirements

Do this assignment using an If/Else statement.

  

Testing

Perform the test run 1 and the test run 2 below with the data shown.

(User input is shown in bold).

(If your output does not contain a decimal point but have the same values, it's OK.)

Test Run 1

Enter the First Number

12.0

Enter a Different Second Number

15.0

First number: 12.0

Second number: 15.0

Larger number: 15.0

Test Run 2

Enter the First Number

15.0

Enter a Different Second Number

12.0

First number: 15.0

Second number: 12.0

Larger number: 15.0

Submit

Copy the following in a file and submit the file:

Output of the test runs.

Source code. (Content of file containing C/C++ code).

Sample Code

//declare variables

double n1, n2, larger;

//write code below to input two numbers from the user in variables n1, n2

// The following code determines which number is largest among n1 or n2

// and stores the largest number in variable largest

if (n1 >= n2) {

            larger = n1;

}

else {

            larger = n2;

}

//Alternative format:

//When there is a single statement inside a pair of braces as above,

//the pair of braces can be skipped as below:

if (n1 >= n2)

            larger = n1;

else

            larger = n2;

Homework Answers

Answer #1
#include <iostream>

using namespace std;

int main(){
   double n1,n2,larger;
   cout<<"Enter the First Number\n";
   cin>>n1;
   cout<<"Enter a Different Second Number\n";
   cin>>n2;
   cout<<"First number: "<<n1<<endl;
   cout<<"Second number: "<<n2<<endl;
   if (n1 >= n2) {
        larger = n1;
   }
   else {
      larger = n2;
   }
   cout<<"Larger number: "<<larger<<endl;
   return 0;
}
Know the answer?
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
Similar Questions
c++ class problem Topics if/else if Description Write a program that calculates a sales person’s monthly...
c++ class problem Topics if/else if Description Write a program that calculates a sales person’s monthly commission. The program asks the user to enter the total sales amount for the month. It calculates the commission on the basis of the sales amount. Then, it displays a report including the sales amount and the commission earned. The commission is computed using the following: 15% commission for the first $2,000.00 sales 20% commission for the next $1,000.00 sales 25% commission for the...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
Problems . Using the if...else if...else if......else, write a C++ program that - Asks the user...
Problems . Using the if...else if...else if......else, write a C++ program that - Asks the user to input two integers. - If both of them are greater than zero, then output their sum. - If only one integer is greater than zero, then output their difference. - Otherwise, output a message "your two number <0".
Q :     Write a C++ program that asks the user to enter three integers and...
Q :     Write a C++ program that asks the user to enter three integers and then displays the largest one. Example : if the user enter ( 7, 2 ,5) the largest number will be 7 Or if user enter ( 2 , 5 , 7 ) the largest number will be 7 Or if user enter ( 7, 5 ,2) the largest number will be 7 In general it does not matter the order of the entered numbers...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the 10 numbers and the average of the 10 numbers please use file i/o and input measures for Handling Errors in C++ When Opening a File
Develop a C++ program that determines the largest and second largest positive values in a collection...
Develop a C++ program that determines the largest and second largest positive values in a collection of data Prompt the user to enter integer values until they enter any negative value to quit You may presume the user will input at least two valid integer values Create a loop structure of some sort to execute this input cycle Maintain the largest and second largest integer as the user inputs data This logic should be placed inside your loop structure Arrays...
write a C++ program that asks the user to enter 4 integer numbers then use if...
write a C++ program that asks the user to enter 4 integer numbers then use if ….. elseif ….. elseif ….. else to find the smallest number then display it.
python Write a program to find the largest value. Ask the user to enter three numbers....
python Write a program to find the largest value. Ask the user to enter three numbers. Compare them and report the largest one. [Hint: The user is free to enter any three numbers. Some or all of them may be the same. Take this into consideration when you compare the numbers.] The following are some examples. Enter first number: 7 Enter second number: 14 Enter third number: 10.5 The largest number is: 14.0 Enter first number: 17 Enter second number:...
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT