Class 12 Python Programs with Output
In this article, you can find some frequently asked Python programs for Class 12 with correct answers.
Problem #1: Leap Year
Write a program to input a year and check whether the year is Leap year or not.
def leapYear(year): # condition for checking whether a year is leap year or not if((year%4 == 0 and year%100 != 0) or (year%400 == 0)): print(str(year) + ' is a leap year') else: print(str(year) + ' is not a leap year') year = int(input('Enter a year: ')) leapYear(year)
Output:
Enter a year: 1996 1996 is a leap year Enter a year: 2000 2000 is a leap year Enter a year: 2010 2010 is not a leap year
Problem #2: Fibonacci series
To write a program to print Fibonacci series upto n terms, also find the sum of series.
def fibonacci(n): series = [] summ = 0 a, b = 0, 1 if(n == 1): series.append(a) summ = 1 else: if(n == 2): series.extend((a,b)) summ = a+b else: series.extend((a,b)) summ = a+b count = 2 while(count < n): c = a + b summ += c series.append(c) a = b b = c count += 1 print(*series) print('Sum of terms = ', summ) n = int(input('Enter no of terms: ')) print('\nFibonacci series upto {} terms\n'.format(n)) fibonacci(n)
Output:
Enter no of terms: 10 Fibonacci series upto 10 terms 0 1 1 2 3 5 8 13 21 34 Sum of terms = 88
Problem #3: Greatest among 3 numbers
Write a program to input 3 numbers and print the greatest number using nested if
p = int(input("Enter first number: ")) q = int(input("Enter second number: ")) r = int(input("Enter third number: ")) if p > q: if p > r: greatest = p else: greatest = r else: if q > r: greatest = q else: greatest = r print('The greatest among {}, {}, {} is {}'.format(p, q, r, greatest))
Output:
Enter first number: 3 Enter second number: 7 Enter third number: 5 The greatest among 3, 7, 5 is 7
Problem #4: Prime number check
Write a program to input a number and check whether it is prime number or not
n = int(input('Enter a number: ')) if n > 1: for i in range(2, n): if n%i == 0: print('{} is not a prime number'.format(n)) break else: print('{} is a prime number'.format(n)) else: print('{} is not a prime number'.format(n))
Output:
Enter a number: 19 19 is a prime number
Problem #5: Number of vowels
Write a program to print a string and the number of vowels present in it
string = input('Enter a string: ') count = 0 vowels = ['a', 'e', 'i', 'o', 'u'] for i in string.lower(): if i in vowels: count += 1 print('\nInput string: ', string) print('\nNumber of vowels in the string = ', count)
Output:
Enter a string: Pythonpoint Input string: Pythonpoint Number of vowels in the string = 3
Problem #6: Number pattern
Write a program to print the following pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
print('---Number pattern---\n') n = int(input("Enter a number: ")) for i in range(1, n+1): for j in range(1, i+1): print(j, end = ' ') print()
Output:
---Number pattern--- Enter a number: 8 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8
Problem #7: Palindrome check
Write a program to check whether the given number is palindrome or not
n = int(input('Enter a number: ')) reverse = 0 temp = n while(temp > 0): reverse = (reverse*10) + (temp%10) temp = temp // 10 if reverse == n: print('{} is a palindrome'.format(n)) else: print('{} is not a palindrome'.format(n))
Output:
Enter a number: 1232 1232 is not a palindrome Enter a number: 1221 1221 is a palindrome
Problem #8: Factorial using Recursion
Write a program to find the factorial of a number using the concept of recursion
def factorial(n): if n == 1: return n else: return n*factorial(n-1) num = int(input("Enter a number: ")) print("The factorial of {} is {}".format(num, factorial(num)))
Output:
Enter a number: 6 The factorial of 6 is 720
Problem #9: Stack operations
Write a menu based program to demonstrate the operations on Stack
def isempty(st): if len(st) == 0: return True else: return False def push(st, n): st.append(n) def pop(st): if isempty(st): print('Underflow condition') else: print('Deleted item: ', st.pop()) def display(st): if isempty(st): print('Stack is empty') else: for i in range(-1, -len(st), -1): if i == -1: print('Top->', st[i]) else: print(' ', st[i]) stack = [] while True: print('---Stack Operations---') print('\n1.Push\n2.Pop\n3.Display\n4.Quit\n') choice = int(input('Enter a choice: ')) if choice == 1: num = int(input("enter the element to be pushed: ")) push(stack, num) print('Push operation successful\n') elif choice == 2: pop(stack) print('Pop operation successful\n') elif choice == 3: display(stack) else: exit()
Output:
---Stack Operations--- 1.Push 2.Pop 3.Display 4.Quit Enter a choice: 1 enter the element to be pushed: 3 Push operation successful ---Stack Operations--- 1.Push 2.Pop 3.Display 4.Quit Enter a choice: 2 Deleted item: 3 Pop operation successful ---Stack Operations--- 1.Push 2.Pop 3.Display 4.Quit Enter a choice: 1 enter the element to be pushed: 54 Push operation successful ---Stack Operations--- 1.Push 2.Pop 3.Display 4.Quit Enter a choice: 1 enter the element to be pushed: 43 Push operation successful ---Stack Operations--- 1.Push 2.Pop 3.Display 4.Quit Enter a choice: 3 Top-> 43 54 ---Stack Operations--- 1.Push 2.Pop 3.Display 4.Quit Enter a choice: 4
Problem #10: Sum of digits
Write a function to accept a number and return the sum of its digits
def sumDigits(n): summ = 0 while n>0 : rem = n%10 summ += rem n //= 10 return summ n = int(input('Enter a number: ')) print('Sum of digits of {} is {}'.format(n, sumDigits(n)))
Output:
Enter a number: 14235 Sum of digits of 14235 is 15