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;
#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; }
Get Answers For Free
Most questions answered within 1 hours.