KEMBAR78
Class12 CS Important Programs Complete | PDF | Applied Mathematics | Theoretical Computer Science
0% found this document useful (0 votes)
6 views5 pages

Class12 CS Important Programs Complete

Uploaded by

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

Class12 CS Important Programs Complete

Uploaded by

Sibling War
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Class 12 CBSE Computer Science: Important Programs

Index
1. Calculate Factorial (Recursion)

2. Palindrome Checker

3. Binary Search (Recursive)

4. Insertion Sort

5. Fibonacci Sequence (Recursion)

6. File Handling (Read and Write)

7. Count Vowels in a String

8. Simple Interest Calculator

9. Armstrong Number Checker

10. Prime Number Checker

11. Merge Two Sorted Arrays

12. Matrix Addition

13. Find Largest Element in a List

14. Queue Implementation using List

15. Stack Implementation using List

1. Calculate Factorial (Recursion)


def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n-1)
print(factorial(5)) # Example

2. Palindrome Checker
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("radar")) # Example
3. Binary Search (Recursive)
def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
return -1
arr = [2, 3, 4, 10, 40]
x = 10
print(binary_search(arr, 0, len(arr)-1, x)) # Example

4. Insertion Sort
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
arr = [12, 11, 13, 5, 6]
insertion_sort(arr)
print(arr) # Example

5. Fibonacci Sequence (Recursion)


def fibonacci(n):
if n <= 0:
return 0
elif n == 1:
return 1
return fibonacci(n-1) + fibonacci(n-2)
for i in range(10):
print(fibonacci(i), end=" ") # Example

6. File Handling (Read and Write)


with open("sample.txt", "w") as file:
file.write("Hello, this is a test file.")
with open("sample.txt", "r") as file:
print(file.read()) # Example
7. Count Vowels in a String
def count_vowels(s):
vowels = 'aeiouAEIOU'
return sum(1 for char in s if char in vowels)
print(count_vowels("Hello World")) # Example

8. Simple Interest Calculator


def simple_interest(principal, rate, time):
return (principal * rate * time) / 100
print(simple_interest(1000, 5, 2)) # Example

9. Armstrong Number Checker


def is_armstrong(n):
digits = [int(d) for d in str(n)]
return n == sum(d ** len(digits) for d in digits)
print(is_armstrong(153)) # Example

10. Prime Number Checker


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
print(is_prime(29)) # Example

11. Merge Two Sorted Arrays


def merge_sorted_arrays(arr1, arr2):
i=j=0
result = []
while i < len(arr1) and j < len(arr2):
if arr1[i] < arr2[j]:
result.append(arr1[i])
i += 1
else:
result.append(arr2[j])
j += 1
result.extend(arr1[i:])
result.extend(arr2[j:])
return result
print(merge_sorted_arrays([1, 3, 5], [2, 4, 6])) # Example
12. Matrix Addition
def matrix_addition(matrix1, matrix2):
result = [[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))] for i in
range(len(matrix1))]
return result
matrix1 = [[1, 2], [3, 4]]
matrix2 = [[5, 6], [7, 8]]
print(matrix_addition(matrix1, matrix2)) # Example

13. Find Largest Element in a List


def find_largest(arr):
return max(arr)
print(find_largest([1, 2, 3, 4, 5])) # Example

14. Queue Implementation using List


class Queue:
def __init__(self):
self.queue = []
def enqueue(self, item):
self.queue.append(item)
def dequeue(self):
if not self.is_empty():
return self.queue.pop(0)
def is_empty(self):
return len(self.queue) == 0
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
print(queue.dequeue()) # Example

15. Stack Implementation using List


class Stack:
def __init__(self):
self.stack = []
def push(self, item):
self.stack.append(item)
def pop(self):
if not self.is_empty():
return self.stack.pop()
def is_empty(self):
return len(self.stack) == 0
stack = Stack()
stack.push(1)
stack.push(2)
print(stack.pop()) # Example

You might also like