Python Programs: Arithmetic Operations & Number Handling
Program to Find the Average of Three Numbers
This program calculates the average of three numbers entered by the user:
number1 = int(input("Please enter the first number: "))
number2 = int(input("Please enter the second number: "))
number3 = int(input("Please enter the third number: "))
result = (number1 + number2 + number3) / 3
print("The average of the three numbers is:", result)
Output:
Please enter the first number: 100 Please enter the second number: 70 Please enter the third number: 80 The average of the three numbers is: 83.33333333333333
Program to Find the Larger of Two Numbers
This program takes two numbers as input and displays the larger number:
no1 = int(input("Enter the first number: "))
no2 = int(input("Enter the second number: "))
if no1 > no2:
print(no1, "is larger than", no2)
elif no1 == no2:
print(no1, "is equal to", no2)
else:
print(no2, "is larger than", no1)
Output:
Enter the first number: 12 Enter the second number: 67 67 is larger than 12
Program to Find the Largest of Three Numbers
This program takes three numbers as input and displays the largest number:
n1 = int(input("Enter the first number: "))
n2 = int(input("Enter the second number: "))
n3 = int(input("Enter the third number: "))
if n1 > n2 and n1 > n3:
print(n1, "is largest")
elif n2 > n1 and n2 > n3:
print(n2, "is largest")
elif n2 == n1 and n3 == n1:
print("All given numbers are equal")
else:
print(n3, "is largest")
Output:
Enter the first number: 15 Enter the second number: 32 Enter the third number: 7 32 is largest
Program to Calculate Area or Perimeter of a Circle
This program calculates either the area or the perimeter of a circle based on user choice:
r = float(input("Enter the radius: "))
print("1. Area of the Circle")
print("2. Perimeter of the Circle")
op = int(input("Choose your Option: "))
if op == 1:
a = 3.14 * r * r
print("Area of the Circle with radius", r, "is", a)
elif op == 2:
p = 2 * 3.14 * r
print("Perimeter of the Circle with radius", r, "is", p)
else:
print("Wrong Option")
Output:
Enter the radius: 25 1. Area of the Circle 2. Perimeter of the Circle Choose your Option: 1 Area of the Circle with radius 25.0 is 1962.5
Simple Calculator Program
This program creates a simple calculator that performs four basic arithmetic operations:
n1 = int(input("Enter the first number: "))
n2 = int(input("Enter the second number: "))
op = input("Choose the operator (+, -, *, **, /, //, %): ")
if op == "+":
res = n1 + n2
elif op == "-":
res = n1 - n2
elif op == "*":
res = n1 * n2
elif op == "**":
res = n1 ** n2
elif op == "/":
if n2 == 0:
res = "Division By Zero"
else:
res = n1 / n2
elif op == "//":
if n2 == 0:
res = "Division By Zero"
else:
res = n1 // n2
elif op == "%":
res = n1 % n2
else:
print("Wrong Option")
print("The result of", n1, op, n2, "=", res)
Output:
Enter the first number: 25 Enter the second number: 5 Choose the operator (+, -, *, **, /, //, %): ** The result of 25 ** 5 = 9765625
Program to Sort Three Numbers in Ascending Order
This program reads three integers and prints them in ascending order:
num1 = int(input("Enter First number: "))
num2 = int(input("Enter Second number: "))
num3 = int(input("Enter Third number: "))
if num1 < num2 and num1 < num3:
if num2 < num3:
x, y, z = num1, num2, num3
else:
x, y, z = num1, num3, num2
elif num2 < num1 and num2 < num3:
if num1 < num3:
x, y, z = num2, num1, num3
else:
x, y, z = num2, num3, num1
else:
if num1 < num2:
x, y, z = num3, num1, num2
else:
x, y, z = num3, num2, num1
print("Numbers in ascending order are:", x, y, z)
Output:
Enter First number: 24 Enter Second number: 12 Enter Third number: 65 Numbers in ascending order are: 12 24 65
Program to Calculate Sum of Even and Odd Integers
This program calculates and prints the sums of even and odd integers up to a given limit:
limit = int(input("Please enter the limit: "))
even_Sum = 0
odd_Sum = 0
for num in range(1, limit + 1):
if num % 2 == 0:
even_Sum = even_Sum + num
else:
odd_Sum = odd_Sum + num
print("The sum of Even numbers from 1 to", limit, "is", even_Sum)
print("The sum of Odd numbers from 1 to", limit, "is", odd_Sum)
Output:
Please enter the limit: 6 The sum of Even numbers from 1 to 6 is 12 The sum of Odd numbers from 1 to 6 is 9
