Class_12_CS_Programs
Class_12_CS_Programs
Question 1:
Write a program in Python to calculate the factorial of a number entered by the user.
Answer:
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
Question 2:
Write a program in Python to calculate the sum of the first N natural numbers.
Answer:
def sum_of_natural_numbers(n):
return n * (n + 1) // 2
Question 3:
Write a Python program to check whether a number is prime or not.
Answer:
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
Question 4:
Write a program in Python to reverse a string entered by the user.
Answer:
def reverse_string(s):
return s[::-1]
Question 5:
Write a Python program to implement a binary search algorithm on a sorted list.
Answer: