Class 12 Python Codebook - Important Programs
1. Find Area of Triangle (Heron's Formula)
Code:
a = float(input("Enter first side: "))
b = float(input("Enter second side: "))
c = float(input("Enter third side: "))
s = (a + b + c) / 2
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
print("Area of triangle:", area)
Sample Output:
Input: 5, 6, 7
Output: Area of triangle: 14.696938456699069
2. Check if Number is Even or Odd
Code:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
Sample Output:
Input: 7
Output: Odd
3. Check Prime Number
Code:
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")
Sample Output:
Input: 13
Output: Prime
4. Print Multiplication Table
Code:
Class 12 Python Codebook - Important Programs
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i, "=", num*i)
Sample Output:
Input: 5
Output: 5 x 1 = 5 ... 5 x 10 = 50
5. Factorial using Loop
Code:
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)
Sample Output:
Input: 5
Output: Factorial: 120
6. Check Palindrome Number
Code:
num = int(input("Enter number: "))
temp = num
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num //= 10
if temp == rev:
print("Palindrome")
else:
print("Not Palindrome")
Sample Output:
Input: 121
Output: Palindrome
7. Count Vowels in a String
Code:
s = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for char in s:
if char in vowels:
count += 1
print("Vowel count:", count)
Class 12 Python Codebook - Important Programs
Sample Output:
Input: Hello
Output: Vowel count: 2
8. Simple Calculator
Code:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /): ")
if op == "+":
print("Result:", a + b)
elif op == "-":
print("Result:", a - b)
elif op == "*":
print("Result:", a * b)
elif op == "/":
print("Result:", a / b)
else:
print("Invalid operator")
Sample Output:
Input: 10, 5, *
Output: Result: 50.0
9. Sum of Digits of a Number
Code:
num = int(input("Enter number: "))
sum = 0
while num > 0:
sum += num % 10
num //= 10
print("Sum of digits:", sum)
Sample Output:
Input: 123
Output: Sum of digits: 6
10. Reverse a String
Code:
s = input("Enter string: ")
rev = s[::-1]
print("Reversed string:", rev)
Sample Output:
Input: Python
Output: Reversed string: nohtyP
Class 12 Python Codebook - Important Programs
11. Check Armstrong Number
Code:
num = int(input("Enter number: "))
temp = num
result = 0
while num > 0:
digit = num % 10
result += digit ** 3
num //= 10
if temp == result:
print("Armstrong Number")
else:
print("Not Armstrong")
Sample Output:
Input: 153
Output: Armstrong Number
12. Fibonacci Series up to n terms
Code:
n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
Sample Output:
Input: 5
Output: 0 1 1 2 3