In this guide, you will learn about the Python program that inputs three different integers from the keyboard, and then prints the sum, the average, the product, the smallest, and the largest of these numbers.
We will explore a Python program that takes three different integers as input from the keyboard and performs various calculations on them.
We will calculate the sum, average, product, smallest, and largest of these numbers.
Python is a powerful programming language known for its simplicity and versatility, making it an excellent choice for beginners and experienced developers alike.
By understanding and implementing this program, you will gain a deeper understanding of basic arithmetic operations and how they can be performed using Python.
Section 1
Python Program: Sum, Average, Product, Smallest, and Largest
Here is the complete Python program that inputs three different integers from the keyboard, and then prints the sum, the average, the product, the smallest, and the largest of these numbers.
Python program that inputs three different integers from the keyboard, and then prints the sum, the average, the product, the smallest, and the largest of these numbers
# Python program to calculate the sum, average, product, smallest, and largest of three integers
# Function to calculate the sum of three integers
def calculate_sum(a, b, c):
return a + b + c
# Function to calculate the average of three integers
def calculate_average(a, b, c):
return (a + b + c) / 3
# Function to calculate the product of three integers
def calculate_product(a, b, c):
return a * b * c
# Function to find the smallest of three integers
def find_smallest(a, b, c):
return min(a, b, c)
# Function to find the largest of three integers
def find_largest(a, b, c):
return max(a, b, c)
# Input three integers from the keyboard
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
c = int(input("Enter the third integer: "))
# Calculate the sum, average, product, smallest, and largest of the three integers
sum_result = calculate_sum(a, b, c)
average_result = calculate_average(a, b, c)
product_result = calculate_product(a, b, c)
smallest_result = find_smallest(a, b, c)
largest_result = find_largest(a, b, c)
# Print the results
print(f"Sum: {sum_result}")
print(f"Average: {average_result}")
print(f"Product: {product_result}")
print(f"Smallest: {smallest_result}")
print(f"Largest: {largest_result}")
You can run this code on our free Online Python Compiler.
Output
Enter the first integer: 10
Enter the second integer: 20
Enter the third integer: 30
Sum: 60
Average: 20.0
Product: 6000
Smallest: 10
Largest: 30
Now that we have the Python program in place, let’s break down the different components and understand how they work.
Breakdown
Code Break Down: Understanding the program
Inputting Three Different Integers
The first step in the program is to input three different integers from the keyboard.
We achieve this using the input() function in Python. The input() function prompts the user to enter a value, and we can store that value in a variable.
In our program, we use the int() function to convert the input values to integers since we are dealing with numbers.
By doing so, we ensure that the input is treated as a numerical value rather than a string.
# Input three integers from the keyboard
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
c = int(input("Enter the third integer: "))
Calculating the Sum
To calculate the sum of three integers, we define a function called calculate_sum(a, b, c).
This function takes three integer arguments (a, b, and c) and returns their sum by using the addition operator (+).
We call this function and pass the input values to it, storing the result in a variable named sum_result.
# Function to calculate the sum of three integers
def calculate_sum(a, b, c):
return a + b + c
Calculating the Average
To calculate the average of three integers, we define a function called calculate_average(a, b, c).
This function takes three integer arguments (a, b, and c) and returns their average by adding them together and dividing the sum by 3.
We call this function and pass the input values to it, storing the result in a variable named average_result.
# Function to calculate the average of three integers
def calculate_average(a, b, c):
return (a + b + c) / 3
Calculating the Product
To calculate the product of three integers, we define a function called calculate_product(a, b, c).
This function takes three integer arguments (a, b, and c) and returns their product by using the multiplication operator (*).
We call this function and pass the input values to it, storing the result in a variable named product_result.
# Function to calculate the product of three integers
def calculate_product(a, b, c):
return a * b * c
Finding the Smallest
To find the smallest of three integers, we define a function called find_smallest(a, b, c).
This function takes three integer arguments (a, b, and c) and uses the min() function to determine the smallest value among them.
We call this function and pass the input values to it, storing the result in a variable named smallest_result.
# Function to find the smallest of three integers
def find_smallest(a, b, c):
return min(a, b, c)
Finding the Largest
To find the largest of three integers, we define a function called find_largest(a, b, c).
This function takes three integer arguments (a, b, and c) and uses the max() function to determine the largest value among them.
We call this function and pass the input values to it, storing the result in a variable named largest_result.
# Function to find the largest of three integers
def find_largest(a, b, c):
return max(a, b, c)
Printing the Results
After performing all the calculations, we print the results using the print() function.
We use f-strings (formatted strings) to display the values of variables within the string.
By placing the variable names inside curly braces ({}), we can incorporate their values into the output message.
Now that we have explained the program’s logic and functionality, let’s address some frequently asked questions.
FAQs
Frequently Asked Questions
What if I enter non-integer values as input?
If you enter non-integer values as input, you will encounter a ValueError.
The program expects integers as input, so make sure to enter valid integer values to avoid errors.
Can I input negative numbers?
Yes, you can input negative numbers.
The program will perform the calculations correctly regardless of whether the input values are positive or negative.
What happens if two or more input values are the same?
The program is designed to work with three different integers.
If you enter two or more identical values, the calculations will still be accurate, but the smallest and largest values will be the same.
Is there a limit to the size of the input integers?
There is no inherent limit to the size of the input integers in the program.
Python supports integers of arbitrary size, so you can enter large numbers without any issues.
Can I modify the program to work with more than three integers?
Yes, you can modify the program to work with a different number of integers.
However, you will need to adjust the functions accordingly to handle the additional values.
Can I reuse this program in other Python projects?
Absolutely! This program can serve as a useful utility for performing basic calculations on three integers.
You can incorporate it into larger projects or modify it to suit your specific needs.
Wrapping Up
Conclusions: Python program that inputs three different integers from the keyboard, and then prints the sum, the average, the product, the smallest, and the largest of these numbers
In conclusion, the Python program that inputs three different integers from the keyboard and calculates the sum, average, product, smallest, and largest values provides a practical way to perform basic arithmetic operations.
By utilizing functions and built-in Python operators, we can achieve accurate and efficient results.
Understanding this program will enhance your knowledge of Python’s syntax, input handling, and arithmetic operations.
Feel free to customize and expand upon this program to suit your unique requirements.
Remember, programming is all about problem-solving and creativity.
With Python, you have a powerful tool at your disposal to bring your ideas to life.
So, start exploring, experimenting, and creating amazing things with Python!
Happy Coding!
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.