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
  • Write a program that asks the user to enter two numbers. Then compare the two numbers....

    Write a program that asks the user to enter two numbers. Then compare the two numbers. Display either:     "The first number is larger", or     "The second number is larger", or     "The numbers are the same".

  • WRITE THIS PROGRAM IN JAVA CODING ONLY ! Program 1: In Order Using an IF/ELSE IF/ELSE...

    WRITE THIS PROGRAM IN JAVA CODING ONLY ! Program 1: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt the user for three numbers and displays them in ascending order. First, the program will prompt the user for each of the three numbers. Next, find the smallest value of the three. Then decide which of the other two is the next smallest. Then have the program display the three numbers in ascending order. Be sure to...

  • 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

  • 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:...

  • 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...

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