Question

Topics If/Else statement Description    Write a program that determines the larger of the two numbers...

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

The 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;

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

Since you have not provided the language of your preference, I am providing the code in C++.

CODE

#include <iostream>

using namespace std;

int main() {

//declare variables

double n1, n2, larger;

cout << "Enter the First Number: ";

cin >> n1;

cout << "Enter a Different Second Number: ";

cin >> 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;

}

cout << "First number: " << n1 << endl;

cout << "Second number: " << n2 << endl;

cout << "Larger number: " << larger << endl;

}

Add a comment
Know the answer?
Add Answer to:
Topics If/Else statement Description    Write a program that determines the larger of the two numbers...
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
  • MATLAB QUESTION 8) Create a program that first prompts the user to enter any two numbers....

    MATLAB QUESTION 8) Create a program that first prompts the user to enter any two numbers. Then prompt the user again to input as many numbers as they choose. Stop the program by inputting-1. Then, display the largest number and second largest number in the command window once the user kills the program. The built-in max function cannot be used. The figure below is an example of what the command window should look like when the code is ran. First...

  • Write a Java program that first reads a positive integer from the user - let's call...

    Write a Java program that first reads a positive integer from the user - let's call it howMany. Then the program reads howMany pairs of integers - let's call them n1 and n2. For each pair, the program determines if the first component in the pair is a multiple of the second component, in other words, if n1 is a multiple of n2. For example, if numberOfPair is 5, your program will read 5 pairs of integers, and for each...

  • 1. Write a program that asks the user to enter two integers, obtains the numbers from...

    1. Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words “is larger.” If the numbers are equal, print the message “These numbers are equal.” Use only the single-selection form of the if statement you learned in this chapter.

  • Topics c ++ Loops While Statement Description Write a program that will display a desired message...

    Topics c ++ Loops While Statement Description Write a program that will display a desired message the desired number of times. The program will ask the user to supply the message to be displayed. It will also ask the user to supply the number of times the message is to be displayed. It will then display that message the required number of times. Requirements Do this assignment using a While statement.    Testing For submitting, use the data in the...

  • Write a C program that asks the user to enter two real numbers. Then your program...

    Write a C program that asks the user to enter two real numbers. Then your program displays a menu that asks the user to choose what arithmetic operation to be done on those numbers. Depending on the user's entry, the program should display the result to the screen. The sample runs below show what should be done to the numbers entered by the user. Your program should run exactly like shown in the sample runs. make your code run as...

  • Write a program that obtains two numbers from the user and displays the larger of the...

    Write a program that obtains two numbers from the user and displays the larger of the two numbers. Use if-else

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

  • C++ Program 1 – Write a program that determines if a person is an adult. Declare...

    C++ Program 1 – Write a program that determines if a person is an adult. Declare a string variable, firstName. Declare an integer variable, age. Asks the user to enter their first name, and store it in the firstName variable. Asks the user to enter their age in years, and store it in the age variable. If the age is greater or equal to 18, the print out their name and “you are an adult” Else print out their name...

  • EXERCISES: if & if...else STATEMENTS: Name Examples of if and if-else statements: Write an if statement...

    EXERCISES: if & if...else STATEMENTS: Name Examples of if and if-else statements: Write an if statement that multiplies payRate by 1.5 iſ hours is greater than 40. Write variable declarations for the variables payRate and hours first. Hint: Look for keyword if first, your boolean comes after the keyword [. This translates into Java as: double hours, payRate; if (hours > 40) { //{-} are optional here but I recommend that you them payRate = payRate 1.5; pay Rato pay...

  • Write a program that will check a Java file for syntax errors. The program will ask...

    Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....

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
ADVERTISEMENT