0% found this document useful (0 votes)
26 views5 pages

Programs For Class 12

4 programming language is there

Uploaded by

chillx444
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)
26 views5 pages

Programs For Class 12

4 programming language is there

Uploaded by

chillx444
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/ 5

PROGRAMS FOR CLASS 12

1. Write a Program to show whether entered numbers are prime or not in the given range.

lower=int(input("Enter lowest number as lower bound to check : "))


upper=int(input("Enter highest number as upper bound to check: "))
c=0
for i in range(lower, upper+1):
if (i == 1):
continue

# flag variable to tell if i is prime or not


flag = 1

for j in range(2, i // 2 + 1):


if (i % j == 0):
flag = 0
break

# flag = 1 means i is prime


# and flag = 0 means i is not prime
if (flag == 1):
print(i, end = " ")

# flag = 1 means i is prime


# and flag = 0 means i is not prime
if (flag == 1):
print(i, end = " ")

2. Input a string and determine whether it is a palindrome or not.

string=input('Enter a string:')
length=len(string)
mid=length//2
rev=-1
for a in range(mid):
if string[a]==string[rev]:
print(string,'is a palindrome.')
break
else:
print(string,'is not a palindrome.')
3. Find the largest/smallest number in a list/tuple

# creating empty list


list1 = []
# asking number of elements to put in list
num = int(input("Enter number of elements in list: "))

# iterating till num to append elements in list


for i in range(1, num + 1):
ele= int(input("Enter elements: "))
list1.append(ele)

# print maximum element


print("Largest element is:", max(list1))

# print minimum element


print("Smallest element is:", min(list1))

4. WAP to input any two tuples and swap their values.


t1 = tuple()
n = int (input("Total no of values in First tuple: "))
for i in range(n):
a = input("Enter Elements : ")
t1 = t1 + (a,)
t2 = tuple()
m = int (input("Total no of values in Second tuple: "))
for i in range(m):
a = input("Enter Elements : ")
t2 = t2 + (a,)
print("First Tuple : ")
print(t1)
print("Second Tuple : ")
print(t2)

t1,t2 = t2, t1

print("After Swapping: ")


print("First Tuple : ")
print(t1)
print("Second Tuple : ")
print(t2)

5. WAP to store students’ details like admission number, roll number, name and percentage in
a dictionary and display information on the basis of admission number.

record = dict ()
i=1
n= int (input ("How many records u want to enter: "))
while(i<=n):
Adm = input("Enter Admission number: ")
roll = input("Enter Roll Number: ")
name = input("Enter Name :")
perc = float(input("Enter Percentage : "))
t = (roll,name, perc)
record[Adm] = t
i=i+1
Nkey = record.keys()
for i in Nkey:
print("\nAdmno- ", i, " :")
r = record[i]
print("Roll No\t", "Name\t", "Percentage\t")
for j in r:
print(j, end = "\t")

6. Write a program with a user-defined function with string as a parameter which replaces all
vowels in the string with ‘*’.
def strep(str):

# convert string into list

str_lst =list(str)

# Iterate list

for i in range(len(str_lst)):

# Each Character Check with Vowels

if str_lst[i] in 'aeiouAEIOU':

# Replace ith position vowel with'*'

str_lst[i]='*'

#to join the characters into a new string.

new_str = "".join(str_lst)
return new_str

def main():
line = input("Enter string: ")
print("Orginal String")
print(line)
print("After replacing Vowels with '*'")
print(strep(line))
main()
7. Recursively find the factorial of a natural number.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

def main():
n = int(input("Enter any number: "))
print("The factorial of given number is: ",factorial(n))
main()
8. Write a recursive code to find the sum of all elements of a list.
def lstSum(lst,n):
if n==0:
return 0
else:
return lst[n-1]+lstSum(lst,n-1)

mylst = [] # Empty List

#Loop to input in list

num = int(input("Enter how many number :"))


for i in range(num):
n = int(input("Enter Element "+str(i+1)+":"))
mylst.append(n) #Adding number to list
sum = lstSum(mylst,len(mylst))
print("Sum of List items ",mylst, " is :",sum)

9. Write a recursive code to compute the nth Fibonacci number.

def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return(fibonacci(n-2) + fibonacci(n-1))

nterms = int(input("Please enter the Range Number: "))

# check if the number of terms is valid


if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(fibonacci(i),end=' ')

10. Read a text file line by line and display each word separated by a #.

filein = open("Mydoc.txt",'r')
line =" "
while line:
line = filein.readline()
#print(line)
for w in line:
if w == ' ':
print('#',end = '')
else:
print(w,end = '')
filein.close()

You might also like