188k views
4 votes
Compare two random numbers and use the if-else statement to output the message displaying which is greater.

User Alkas
by
8.8k points

1 Answer

4 votes

Final answer:

To compare two random numbers and determine which is greater using the if-else statement, generate two random numbers within a specified range, store each number in a variable, and use an if-else statement to compare the numbers.

Step-by-step explanation:

In order to compare two random numbers and determine which is greater using the if-else statement, you can follow these steps:

  1. Generate two random numbers using the specified range, such as randInt(0, 30).
  2. Store the first random number in a variable.
  3. Store the second random number in another variable.
  4. Use an if-else statement to compare the two numbers:
  • If the first number is greater than the second number, output a message stating that the first number is greater.
  • Otherwise, if the second number is greater than the first number, output a message stating that the second number is greater.
  • If both numbers are equal, output a message stating that they are equal.

Here is an example code:

num1 = randInt(0, 30)
num2 = randInt(0, 30)

if num1 > num2:
print("The first number is greater")
elif num2 > num1:
print("The second number is greater")
else:
print("The numbers are equal")

User Eldar Djafarov
by
8.0k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.