0% found this document useful (0 votes)
14 views14 pages

SourceCode1to8_Print

Computer science class 12
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)
14 views14 pages

SourceCode1to8_Print

Computer science class 12
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
You are on page 1/ 14

Source Code:

# Sum of Series
import math
def fact(n):
import math
fact=math.factorial(n)
return fact
def power(x, n):
series_sum = 0
for k in range(n):
numerator = (-1)**k * x**(2*k+1)
denominator = fact(2*k+2)
term = numerator / denominator
series_sum += term
return series_sum
x = int(input("Enter the value of x:"))
n = int(input("Enter the number of terms:"))
result = power(x, n)
print("The sum of the series is: ",result)

print("\nSeries2")
print("*********")
def fact(n):
import math
fact=math.factorial(n)
return fact
def power(x, n):
series_sum = 0
for i in range(n):
numerator = (-1)**i * x**(2*i+1)
denominator = fact(2*i+3)
term = numerator / denominator
series_sum += term
return series_sum
x = int(input("Enter the value of x:"))
n = int(input("Enter the number of terms:"))
result = power(x, n)
print("The sum of the series is: ",result)
Output:
Source Code:

# Statistical Functions

import statistics as st
def stat(l):
mean=st.mean(l)
median=st.median(l)
mode=st.mode(l)
return mean, median, mode
def main_fn():
l=[]
n=int(input("Enter the number of elements in the list: "))
for i in range(n):
ele=int(input("Enter the element:"))
l.append(ele)
print("The list is:",l)
a,b,c=stat(l)
print("\nMean,Median,Mode")
print("Mean is:",a)
print("Median is:",b)
print("Mode is:",c)
main_fn()

Output:
Source Code:

# Menu Driven Code- Numeric

def fibonacci(n):
from fibonacci import fibonacci as fib
return fib(length=n)

def fact(n):
from math import factorial as fact
return fact(n)

print("MENU DRIVEN CODE- NUMERIC ")


while True:
print("\n1. Factorial of a number")
print("2. Generate ‘N’ numbers of Fibonacci series ")
print("3. Exit")
choice=int(input("Enter your choice: "))
if choice==1:
num=int(input("Enter the number to find the factorial for:"))
print("The factorial for",num,"is",fact(num))
elif choice==2:
num=int(input("Enter the number of terms to generate fibonacci series for:"))
print("The finbonacci for",num,"terms is",fibonacci(num))
elif choice==3:
print("Exit")
break

Output:
Source Code:

# Menu Driven Code- List/String

def binary_search(arr, target):


left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
# Check if target is present at mid
if arr[mid] == target:
return mid
# If target is greater, ignore left half
elif arr[mid] < target:
left = mid + 1
# If target is smaller, ignore right half
else:
right = mid - 1
# If we reach here, the element was not present
return -1

def revstr(n):
return n[::-1]

while True:
print("\nMENU DRIVEN CODE- LIST/STRING")
print("1. Binary Search in a list of integers")
print("2. Reversing a string")
print("3. Exit")
choice=int(input("Enter your choice: "))
if choice==1:
list1=eval(input("Enter the list:"))
sorted_list = sorted(list1)
search = int(input("Enter the element to search:"))
print("Sorted List is:",sorted_list)
result = binary_search(sorted_list, search)
if result != -1:
print("Element found at Position",result+1)
else:
print("Element not found")
elif choice==2:
str1=input("Enter the String to reverse: ")
print("The reversed string is: ",revstr(str1))
elif choice==3:
print("Exit")
break
Output:
Source Code:

# Random Number Generation

def prime(l):
l1=[]
for num in l:
for i in range(2, num):
if (num % i) == 0:
break
else:
l1.append(num)
if l1==[]:
print("List of prime numbers are none")
else:
print("Prime number list is:",l1)

def Max_SecMax(l):
l.sort()
print("Sorted list is:",l)
print("Largest number is: ",l[-1])
print("Second Largest number is: ",l[-2])

def generate(ele,low,high):
from random import randint as r
l=[]
for i in range(ele):
x=r(low,high)
l.append(x)
print("The list is: ",l)
Max_SecMax(l)
prime(l)

while True:
print("\nPRIME NUMBER CHECK")
print("1. Generate Random number between the limits")
print("2. Exit")
choice=int(input("Enter your choice: "))
if choice==1:
ele=int(input("Enter the number of items in the list: "))
low=int(input("Enter the lower limit: "))
high=int(input("Enter the higher limit: "))
generate(ele,low,high)
elif choice==2:
print("Exit")
break
Output:

Source Code:

# Menu Driven Code – Tuples


def vow(t):
t2=()
for i in t:
j=i.lower()
if 'a' in j and 'e' in j and 'i' in j and 'o' in j and 'u' in j:
t2=t2+(j,)
return t2

def bmi(name, h, w):


result=''
bmi=round((w/h**2),1)
if bmi>=30:
result='Obese'
elif bmi>25:
result='OverWeight'
elif bmi>=18.5 and bmi<=25:
result='Normal'
elif bmi>=1 and bmi<18.5:
result='Underweight'
return bmi,result

while True:
print("\nMENU DRIVEN CODE – TUPLES")
print("1. Display words with all vowels")
print("2. Check BMI")
print("3. Exit")
choice=int(input("Enter your choice: "))

if choice==1:
t1=eval(input("Enter a tuple to check: "))
print("Tuple of words with vowels:",vow(t1))
elif choice==2:
t1=eval(input("Enter the nested tuple with name, height(m), weight: "))
for i in t1:
a,b=bmi(i[0],i[1],i[2])
print("The BMI of",i[0],":",a,"-",b)
elif choice==3:
print("Exit")
break

Output:
Source Code:
# Menu Driven Program - Dictionary
def add(d):
while True:
name=input("Enter the name:")
phno=int(input("Enter the Ph no:"))
d[name]=phno
choice=input("Do you wish to add more data:Y/N: ")
if choice.lower()!="y":
break

def update(d):
if len(d)==0:
print("Dictionary is empty")
else:
name=input("Enter the name to modify the Phone no: ")
print("Current Phone no. is: ",d[name])
phno=int(input("Enter the new Phone no: "))
d[name]=phno
print("Number Updated")

def delete(d):
name=input("Enter the name to be deleted: ")
if name not in d:
print("Name not in the list")
else:
del d[name]
print("Entry deleted")

while True:
print("\nMENU DRIVEN CODE – DICTIONARY")
print("1. Add")
print("2. View")
print("3. Modify")
print("4. Delete")
print("5. Exit")
choice=int(input("Enter your choice: "))
if choice==1:
d={}
add(d)
elif choice==2:
print(d)
elif choice==3:
update(d)
elif choice==4:
delete(d)
elif choice==5:
print("Exit")
break
Output:
Source Code:
# Nested Dictionary
def house(name):
if name==0:
house="BHARATHI"
elif name==1:
house="TAGORE"
elif name==2:
house="SHIVAJI"
elif name==3:
house="PRATAP"
return house

def seniors(d):
l=[]
for i in d:
l.append(d[i]["Seniors"])
name=l.index(max(l))
print("Seniors:",house(name),max(l))

def juniors(d):
l=[]
for i in d:
l.append(d[i]["Juniors"])
name=l.index(max(l))
print("Juniors:",house(name),max(l))

def subjuniors(d):
l=[]
for i in d:
l.append(d[i]["SubJuniors"])
name=l.index(max(l))
print("SubJuniors:",house(name),max(l))

d=eval((input("Enter the dictionary: ")))


while True:
print("\nNESTED DICTIONARY")
print("1. House with max.score in Seniors Category")
print("2. House with max.score in Juniors Category")
print("3. House with max.score in Sub-Juniors Category")
print("4. Exit")
choice=int(input("Enter your choice: "))
if choice==1:
seniors(d)
elif choice==2:
juniors(d)

elif choice==3:
subjuniors(d)
elif choice==4:
print("Exit")
break
Output:

You might also like