167k views
1 vote
Write a program that takes three numbers as input from the user, and prints the smallest.

Hint: Remember that the numbers should be compared numerically. Any input from the user must be transformed into an integer, but printed as a string.

Sample Run
Enter a number: 20
Enter a number: 50
Enter a number: 5

Smallest: 5
Note: The program is Python

User Ken W
by
8.6k points

1 Answer

3 votes

Answer:

Here's the Python code to achieve the given task:

num1 = int(input("Enter a number: "))

num2 = int(input("Enter a number: "))

num3 = int(input("Enter a number: "))

smallest = num1

if num2 < smallest:

smallest = num2

if num3 < smallest:

smallest = num3

print("Smallest: " + str(smallest))

Step-by-step explanation:

1. We use the input() function to take three numbers as input from the user and convert them to integers using the int() function.

2. We initialize the smallest variable to be the first number entered (num1).

3. We compare smallest to num2 and num3 using if statements and update smallest accordingly if one of these is smaller.

4. Finally, we print the value of smallest as a string using the str() function.

User Josh Ribakoff
by
7.6k points

No related questions found