0% found this document useful (0 votes)
17 views4 pages

Class12 CS Programs Part2

Uploaded by

unfunnyaadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Class12 CS Programs Part2

Uploaded by

unfunnyaadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class 12 Computer Science - Python Programs (Q11 to Q30)

Q11. Check if a character is uppercase, lowercase, digit or symbol.


ch = input("Enter a character: ")
if [Link]():
print("Uppercase letter")
elif [Link]():
print("Lowercase letter")
elif [Link]():
print("Digit")
else:
print("Symbol")

Q12. Print table of a number.


n = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")

Q13. Print sum of natural numbers from 1 to 100.


total = sum(range(1, 101))
print("Sum =", total)

Q14. Calculate factorial of a number.


n = int(input("Enter a number: "))
fact = 1
for i in range(1, n+1):
fact *= i
print("Factorial =", fact)

Q15. Sum of even and odd integers of first n numbers.


n = int(input("Enter n: "))
even_sum = sum(i for i in range(1, n+1) if i % 2 == 0)
odd_sum = sum(i for i in range(1, n+1) if i % 2 != 0)
print("Even sum =", even_sum)
print("Odd sum =", odd_sum)

Q16. Check if a number is prime.


n = int(input("Enter number: "))
if n < 2:
print("Not Prime")
else:
for i in range(2, int(n**0.5)+1):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime")

Q17. Reverse number > 1000.


n = int(input("Enter number > 1000: "))
if n > 1000:
print("Reversed =", str(n)[::-1])
else:
print("Number is not > 1000")

Q18. Print sum of series: s=1+x+x^2+...+x^n.


x = int(input("Enter x: "))
n = int(input("Enter n: "))
s = sum(x**i for i in range(n+1))
print("Sum =", s)

Q19. Check Armstrong number.


n = int(input("Enter number: "))
order = len(str(n))
sum_of_powers = sum(int(d)**order for d in str(n))
print("Armstrong" if n == sum_of_powers else "Not Armstrong")

Q20. Check perfect number.


n = int(input("Enter number: "))
sum_of_div = sum(i for i in range(1, n) if n % i == 0)
print("Perfect" if sum_of_div == n else "Not Perfect")

Q21. Check palindrome number.


n = input("Enter number: ")
print("Palindrome" if n == n[::-1] else "Not Palindrome")

Q22. Fibonacci series up to n terms.


n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

Q23. Print pattern:


(i) Left-aligned triangle of 1s
(ii) Inverted numbers
# (i)
for i in range(1, 6):
print("1 " * i)
# (ii)
for i in range(5, 0, -1):
for j in range(5, 5 - i, -1):
print(j, end=" ")
print()

Q24. Check leap year.


year = int(input("Enter year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not Leap Year")

Q25. Print grade according to percentage.


per = float(input("Enter percentage: "))
if per > 90:
print("Grade A")
elif per > 80:
print("Grade B")
elif per > 70:
print("Grade C")
elif per > 60:
print("Grade D")
else:
print("Grade E")

Q26. Line statistics: uppercase, lowercase, digits, symbols.


line = input("Enter a line: ")
upper = sum(1 for c in line if [Link]())
lower = sum(1 for c in line if [Link]())
alphabets = sum(1 for c in line if [Link]())
digits = sum(1 for c in line if [Link]())
symbols = len(line) - alphabets - digits
print("Uppercase:", upper)
print("Lowercase:", lower)
print("Alphabets:", alphabets)
print("Digits:", digits)
print("Symbols:", symbols)

Q27. Longest substring of consonants.


import re
s = input("Enter string: ")
matches = [Link](r'[^aeiouAEIOU\s]+', s)
longest = max(matches, key=len) if matches else ''
print("Longest consonant substring:", longest)
Q28. Search for element in a list.
lst = list(map(int, input("Enter list elements: ").split()))
x = int(input("Enter element to search: "))
print("Found" if x in lst else "Not found")

Q29. Frequency of a given element in list.


lst = list(map(int, input("Enter list elements: ").split()))
x = int(input("Enter element: "))
print("Frequency:", [Link](x))

Q30. Frequencies of all elements, unique and duplicate elements.


from collections import Counter
lst = list(map(int, input("Enter list elements: ").split()))
freq = Counter(lst)
print("Frequencies:", freq)
print("Unique elements:", [k for k, v in [Link]() if v == 1])
print("Duplicate elements:", [k for k, v in [Link]() if v > 1])

You might also like