Python Programs and Their Outcomes
Name: Shivang Singh
Class: 10th C
Roll No.: 28
Date: 7th January 2025
1. Program to input a year and find if it is a leap year or not
Python Code:
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Expected Outcome:
Input: 2024
Output: 2024 is a leap year.
2. WAP to input sides of a triangle and calculate its area using Heron's formula
Python Code:
import math
a = float(input("Enter the first side of the triangle: "))
b = float(input("Enter the second side of the triangle: "))
c = float(input("Enter the third side of the triangle: "))
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print(f"Area of the triangle = {area}")
Expected Outcome:
Input: a = 3, b = 4, c = 5
Output: Area of the triangle = 6.0
3. Write a program to input a number and determine whether it is a prime
number or not
Python Code:
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print(f"{num} is not a prime number.")
break
else:
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
Expected Outcome:
Input: 7
Output: 7 is a prime number.
4. Write a program to input the principal, rate, and time to calculate simple
interest
Python Code:
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time in years: "))
simple_interest = (principal * rate * time) / 100
print(f"Simple Interest = {simple_interest}")
Expected Outcome:
Input: Principal = 1000, Rate = 5%, Time = 2 years
Output: Simple Interest = 100.0
5. Write a program to input a number and check whether it is even or odd
Python Code:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is an even number.")
else:
print(f"{num} is an odd number.")
Expected Outcome:
Input: 4
Output: 4 is an even number.
6. Write a program to convert a temperature from Celsius to Fahrenheit
Python Code:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"Fahrenheit = {fahrenheit}")
Expected Outcome:
Input: Celsius = 0
Output: Fahrenheit = 32.0
7. WAP to print all even numbers from 1 to 100
Python Code:
for i in range(1, 101):
if i % 2 == 0:
print(i, end=", ")
Expected Outcome:
Output: 2, 4, 6, ..., 100
8. Write a program to input a number and check whether it is positive,
negative, or zero
Python Code:
num = float(input("Enter a number: "))
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
Expected Outcome:
Input: -5
Output: The number is negative.
9. Write a program to input two numbers and swap their values
Python Code:
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
# Swapping values
a, b = b, a
print(f"After swapping: a = {a}, b = {b}")
Expected Outcome:
Input: a = 3, b = 4
Output: a = 4, b = 3
10. Write a program to input a number and display its square
Python Code:
num = int(input("Enter a number: "))
square = num ** 2
print(f"Square of {num} = {square}")
Expected Outcome:
Input: 5
Output: Square of 5 = 25