Practice Problems for logic building in python
Questions:
1. Sum of two numbers
2. Sum of three numbers
3. Square of a number
4. Cube of a number
5. Average of 5 numbers
6. Area of Rectangle (Formula: length * breadth )
7. Area of Circle ( Formula: πr2)
8. Volume of a Sphere (Formula: (4/3) * π * radius3 )
9. Simple Interest
10.Temperature Conversion (degree celsius to degree fahrenheit)
( Formula: Fahrenheit=(Celsius×59)+32 )
Solutions:
1. Sum of Two Numbers
# This program calculates the sum of two numbers provided by the user.
# Taking input for the first number
first_number=float(input("
Enter the first number:") )
# Taking input for the second number
second_number=float(input("Enter the second number:") )
# Calculating the sum
sum_of_two=first_number+second_number
# Displaying the result
print("The sum of the two numbers is:",sum_of_two)
2. Sum of Three Numbers
# This program calculates the sum of three numbers provided by the user.
# Taking input for the first number
first_num=float(input("Enter the first number: "))
# Taking input for the second number
second_num=float(input("
Enter the second number:"))
# Taking input for the third number
third_num=float(input("Enter the third number: "))
# Calculating the sum
sum_of_three=first_num+second_num+third_num
# Displaying the result
print("The sum of the three numbers is:",sum_of_three)
3. Square of a Number
# This program calculates the square of a number provided by the user.
# Taking input for the number
number_to_square=float(input("Enter a number tofind its square: "))
# Calculating the square
square_of_number=number_to_square**2
# Displaying the result
print("The square of the number is:",square_of_number)
4. Cube of a Number
# This program calculates the cube of a number provided by the user.
# Taking input for the number
number_to_cube=float(input("Enter a number to findits cube: "))
# Calculating the cube
cube_of_number=number_to_cube**3
# Displaying the result
print("The cube of the number is:",cube_of_number)
5. Average of 5 Numbers
# This program calculates the average of five numbers provided by the user.
# Taking input for five numbers
num1=float(input("
Enter the first number: "))
num2=float(input("
Enter the second number: "))
num3=float(input("
Enter the third number: "))
num4=float(input("
Enter the fourth number: "))
num5=float(input("
Enter the fifth number: "))
# Calculating the average
average_of_five=(num1+num2+num3+num4+num5)/5
# Displaying the result
print("The average of the five numbers is:",average_of_five)
6. Area of Rectangle
# This program calculates the area of a rectangle given its length and breadth.
# Taking input for the length of the rectangle
rectangle_length=float(input("Enter the length ofthe rectangle: "))
# Taking input for the breadth of the rectangle
rectangle_breadth=float(input("
Enter the breadthof the rectangle: "))
# Calculating the area
area_of_rectangle=rectangle_length*rectangle_breadth
# Displaying the result
print("The area of the rectangle is:",area_of_rectangle)
7. Area of Circle
# This program calculates the area of a circle given its radius.
# Taking input for the radius of the circle
circle_radius=float(input("Enter the radius of thecircle: "))
# Calculating the area (π ≈ 3.14159)
area_of_circle=3.14159*(c ircle_radius**2)
# Displaying the result
print("The area of the circle is:",area_of_circle)
8. Volume of a Sphere
# This program calculates the volume of a sphere given its radius.
# Taking input for the radius of the sphere
sphere_radius=float(input("Enter the radius of thesphere: "))
# Calculating the volume ( π=3.14159)
volume_of_sphere=(4/3 )*3.14159*(s phere_radius**3)
# Displaying the result
print("The volume of the sphere is:",volume_of_sphere)
9. Simple Interest
# This program calculates the simple interest based on the principal amount, rate, and time.
# Taking input for the principal amount
principal_amount=float(input("
Enter the principalamount: "))
# Taking input for the rate of interest (as a percentage)
interest_rate=float(input("Enter the rate of interest(in %): "))
# Taking input for the time (in years)
time_period=float(input("Enter the time period (inyears): "))
# Calculating the simple interest
simple_interest=(principal_amount*interest_rate*time_period)/100
# Displaying the result
print("The simple interest is:",simple_interest)
10. Temperature Conversion (Celsius to Fahrenheit)
# This program converts temperature from degrees Celsius to degrees Fahrenheit.
# Taking input for temperature in Celsius
temperature_celsius=float(input("
Enter temperaturein Celsius: "))
# Calculating the temperature in Fahrenheit
temperature_fahrenheit=(t emperature_celsius*9/5 )+32
# Displaying the result
print
(
temperature_celsius
,
"°C is equal to"
,
temperature_fahrenheit
,
"°F"
)
* For this ° press Alt + 0176 (press Alt and type 0176)