Class12 Python Programs
Class12 Python Programs
Program 1
Question: WAP to compute x^n of given two integers x and n.
Source Code:
x = int(input("Enter base (x): "))
n = int(input("Enter exponent (n): "))
print(f"{x}^{n} =", x**n)
Sample Output:
Enter base (x): 2
Enter exponent (n): 3
2^3 = 8
Program 2
Question: WAP for calculating simple interest.
Source Code:
p = float(input("Enter principal amount: "))
r = float(input("Enter rate of interest: "))
t = float(input("Enter time in years: "))
si = (p * r * t) / 100
print("Simple Interest:", si)
Sample Output:
Enter principal amount: 1000
Enter rate of interest: 5
Enter time in years: 2
Simple Interest: 100.0
Program 3
Question: WAP to accept a number from the user and display whether it is even or odd.
Source Code:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
Sample Output:
Enter a number: 7
7 is Odd
Program 4
Question: WAP to accept percentage of a student and display its grade accordingly.
Source Code:
percentage = float(input("Enter percentage: "))
if percentage >= 90:
grade = 'A'
elif percentage >= 80:
grade = 'B'
elif percentage >= 70:
grade = 'C'
elif percentage >= 60:
grade = 'D'
else:
grade = 'F'
print("Grade:", grade)
Sample Output:
Enter percentage: 85
Grade: B
Program 5
Question: WAP to print Fibonacci series up to a certain limit.
Source Code:
n = int(input("Enter limit: "))
a, b = 0, 1
while a <= n:
print(a, end=" ")
a, b = b, a + b
Sample Output:
Enter limit: 10
0112358
Program 6
Question: WAP to display prime numbers up to a certain limit.
Source Code:
limit = int(input("Enter limit: "))
for num in range(2, limit+1):
for i in range(2, int(num**0.5)+1):
if num % i == 0:
break
else:
print(num, end=" ")
Sample Output:
Enter limit: 10
2357
Program 7
Question: WAP to check if a number is an Armstrong number.
Source Code:
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** len(str(num))
temp //= 10
if num == sum:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
Sample Output:
Enter a number: 153
153 is an Armstrong number
Program 8
Question: WAP to check if a number is a perfect number.
Source Code:
num = int(input("Enter a number: "))
sum = sum(i for i in range(1, num) if num % i == 0)
if sum == num:
print(num, "is a Perfect number")
else:
print(num, "is not a Perfect number")
Sample Output:
Enter a number: 6
6 is a Perfect number
Program 9
Question: WAP to print sum of exponential series 1+x/1!+x^2/2!+...+x^n/n!
Source Code:
import math
x = int(input("Enter x: "))
n = int(input("Enter n: "))
sum = sum((x**i) / math.factorial(i) for i in range(n+1))
print("Sum of series:", sum)
Sample Output:
Enter x: 2
Enter n: 3
Sum of series: 7.0
Program 10
Question: WAP to print a pattern.
Source Code:
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
print(" ".join(map(str, range(1, i+1))))
Sample Output:
Enter number of rows: 3
1
12
123
Program 11
Question: WAP to check if a string is a palindrome.
Source Code:
s = input("Enter a string: ")
if s == s[::-1]:
print(s, "is a Palindrome")
else:
print(s, "is not a Palindrome")
Sample Output:
Enter a string: madam
madam is a Palindrome
Program 12
Question: WAP to count alphabets, digits, uppercase, lowercase, spaces, and other
characters.
Source Code:
s = input("Enter a string: ")
alphabets = digits = upper = lower = spaces = others = 0
for char in s:
if char.isalpha():
alphabets += 1
if char.isupper():
upper += 1
else:
lower += 1
elif char.isdigit():
digits += 1
elif char.isspace():
spaces += 1
else:
others += 1
print(f"Alphabets: {alphabets}, Digits: {digits}, Uppercase: {upper}, Lowercase: {lower},
Spaces: {spaces}, Others: {others}")
Sample Output:
Enter a string: Hello 123!
Alphabets: 5, Digits: 3, Uppercase: 1, Lowercase: 4, Spaces: 1, Others: 1