Program 1: Area & Perimeter of Rectangle
(Using Function)
def rectangle(l, b):
area = l * b
perimeter = 2 * (l + b)
print("Area of Rectangle:", area)
print("Perimeter of Rectangle:", perimeter)
l = int(input("Length : "))
b = int(input("Breadth : "))
rectangle(l, b)
Program 2: Factorial (Using Function)
def factorial(num):
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
f = 1
for i in range(1, num + 1):
f *= i
print("The factorial of", num, "is", f)
num = int(input("Enter a number: "))
factorial(num)
Program 3: Compound Interest (Using
Function)
(Shows final CI & Amount)
def compound_interest(p, rate, years):
amount = p * (1 + rate / 100) ** years
ci = amount - p
print("Compound Interest:", ci)
print("Final Amount:", amount)
p = int(input("Enter principal amount: "))
rate = float(input("Enter rate: "))
years = int(input("Enter years: "))
compound_interest(p, rate, years)
Program 4: Count Digits & Letters (Using
Function)
def count_digits_letters(s):
digits = 0
letters = 0
for i in s:
if [Link]():
digits += 1
elif [Link]():
letters += 1
print("Digits:", digits)
print("Letters:", letters)
s = input("Enter string: ")
count_digits_letters(s)
Program 5: Largest Element in List (Using
Function)
def largest_element(a):
[Link]()
print("Largest element is:", a[-1])
n = int(input("Enter number of elements: "))
a = []
for i in range(n):
[Link](int(input("Enter element: ")))
largest_element(a)
✅ Program 25: Count Digits in a Number
(Using Function)
def count_digits(num):
if num == 0:
return 1
count = 0
while num > 0:
num //= 10
count += 1
return count
num = int(input("Enter a number: "))
print("Number of digits:", count_digits(num))
Program 19: String Palindrome (Using
Function)
def is_palindrome(s):
if s == s[::-1]:
print("The string is a palindrome")
else:
print("The string is not a palindrome")
s = input("Enter string: ")
is_palindrome(s)
Program 18: Prime Number (Using Function)
def is_prime(num):
if num <= 1:
print(num, "is not a prime number")
return
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print(num, "is not a prime number")
return
print(num, "is a prime number")
num = int(input("Enter number: "))
is_prime(num)
Program 23: Leap Year (Using Function)
def leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
year = int(input("Enter a year: "))
leap_year(year)
If you want, I can also combine all programs into one menu-driven Python application.
You said:
with output
ChatGPT said:
Here are all corrected programs using def along with sample outputs for each one.
Program 1: Area & Perimeter of Rectangle
(with Output)
Code
def rectangle(l, b):
area = l * b
perimeter = 2 * (l + b)
print("Area of Rectangle:", area)
print("Perimeter of Rectangle:", perimeter)
l = int(input("Length : "))
b = int(input("Breadth : "))
rectangle(l, b)
Sample Output
Length : 5
Breadth : 3
Area of Rectangle: 15
Perimeter of Rectangle: 16
Program 2: Factorial (with Output)
Code
def factorial(num):
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
f = 1
for i in range(1, num + 1):
f *= i
print("The factorial of", num, "is", f)
num = int(input("Enter a number: "))
factorial(num)
Sample Output
Enter a number: 5
The factorial of 5 is 120
Program 3: Compound Interest (with Output)
def compound_interest(p, rate, years):
amount = p * (1 + rate / 100) ** years
ci = amount - p
print("Compound Interest:", ci)
print("Final Amount:", amount)
p = int(input("Enter principal amount: "))
rate = float(input("Enter rate: "))
years = int(input("Enter years: "))
compound_interest(p, rate, years)
Sample Output
Enter principal amount: 1000
Enter rate: 10
Enter years: 2
Compound Interest: 210.00000000000023
Final Amount: 1210.0000000000002
Program 4: Count Digits & Letters (with
Output)
Code
def count_digits_letters(s):
digits = 0
letters = 0
for i in s:
if [Link]():
digits += 1
elif [Link]():
letters += 1
print("Digits:", digits)
print("Letters:", letters)
s = input("Enter string: ")
count_digits_letters(s)
Sample Output
Enter string: Hello123
Digits: 3
Letters: 5
Program 5: Largest Element in List (with
Output)
Code
def largest_element(a):
[Link]()
print("Largest element is:", a[-1])
n = int(input("Enter number of elements: "))
a = []
for i in range(n):
[Link](int(input("Enter element: ")))
largest_element(a)
Sample Output
Enter number of elements: 4
Enter element: 12
Enter element: 9
Enter element: 25
Enter element: 2
Largest element is: 25
Program 25: Count Digits in a Number (with
Output)
Code
def count_digits(num):
if num == 0:
return 1
count = 0
while num > 0:
num //= 10
count += 1
return count
num = int(input("Enter a number: "))
print("Number of digits:", count_digits(num))
Sample Output
Enter a number: 98765
Number of digits: 5
Program 19: String Palindrome (with Output)
Code
def is_palindrome(s):
if s == s[::-1]:
print("The string is a palindrome")
else:
print("The string is not a palindrome")
s = input("Enter string: ")
is_palindrome(s)
Sample Output
Enter string: madam
The string is a palindrome
Program 18: Prime Number Check (with
Output)
Code
def is_prime(num):
if num <= 1:
print(num, "is not a prime number")
return
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print(num, "is not a prime number")
return
print(num, "is a prime number")
num = int(input("Enter number: "))
is_prime(num)
Sample Output
Enter number: 17
17 is a prime number
Program 23: Leap Year (with Output)
Code
def leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
year = int(input("Enter a year: "))
leap_year(year)
Sample Output
Enter a year: 2024
2024 is a leap year