CLASS : XII RECORD MODEL SUBJECT: COMPUTER SCIENCE (083)
EXPT NO: 1 DISPLAY THE TERMS OF A FIBONACCI SERIES.
Aim:
To write a python program to display the terms of a Fibonacci series.
Algorithm:
- Start the program
- Input: Prompt the user to enter an integer n and store this value in n.
- Initialization of the variable a and b:
- Set a to 0 (the first Fibonacci number).
- Set b to 1 (the second Fibonacci number).
- Output:
- Print the value of a.
- Print the value of b.
- Loop: Iterate from 2 to n - 1 (inclusive):
- Compute the next Fibonacci number c as the sum of a and b (c = a + b).
- Print the value of c.
- Update the values of the variable a and b:
Set a to the old value of b.
Set b to the new value of c.
- Stop the program
Program:
n=int(input("Enter the n value: "))
a=0
b=1 Output: Enter the n value: 5
print(a)
Fibonacci series:
print(b)
0
print(“ Fibonnacci series:”)
1
for i in range(2,n):
1
c=a+b
2
print(c)
3
a=b
b=c
Result:
Hence, the program was successfully executed, and the Fibonacci series was printed correctly.
EXPT NO: 2 CHECKING ARMSTRONG NUMBER OR NOT
Aim:
To write a python program to check whether the given number is an Armstrong number or not.
Algorithm:
Start the program.
Input the Number:
- Prompt the user to enter a number and store it in a variable num.
Initialize Variables:
- Set summ to 0. This variable will be used to accumulate the sum of the cubes of the digits
of the number.
- Copy the value of num to temp. temp will be used for processing the digits of the number.
While temp is greater than 0:
- Find the last digit of temp using the modulus operation (digit = temp % 10).
- Compute the cube of the digit and add it to summ (summ = summ + digit ** 3).
- Remove the last digit from temp by performing integer division by 10 (temp = temp // 10).
Check and Output the Result:
- After the loop, compare the value of summ with num.
- If summ is equal to num, print that the number is an Armstrong number.
- Otherwise, print that the number is not an Armstrong number.
Stop the program
Program:
num=int(input("Enter the number to be checked :"))
summ=0
temp=num Output:
while temp>0: Enter the number to be checked: 153
digit=temp%10 The given number 153 is an Armstrong number
summ=summ+digit**3
temp=temp//10
if summ==num:
print("The given number",num," is an Armstrong number")
else:
print("The given number",num," is not an Armstrong number")
Result:
Hence, the program was successfully executed and verified whether the given number is an
Armstrong number.
EXPT NO: 3 FINDING THE SUM OF THE SERIES
Aim:
To write a python program to find the sum of series: S=1+x+x2+x3+…..+xn
Algorithm:
Start the program.
Input Values:
- Ask the user to input a floating-point/integer number x.
- Ask the user to input an integer n.
Initialize Sum:
- Start with s = 0 to accumulate the sum of terms.
Calculate Sum of Series
- Use a loop to iterate from 0 to n.
- In each iteration, compute x^a (where a is the loop index) and add it to s.
Display Result:
- Print the total sum of the series.
Stop the program
Program:
x=float(input("Enter the value of x :"))
n=int(input("Enter the value of n: "))
s=0
for a in range(n+1):
s=s+x**a
print("sum of first",n,"terms:",s)
Output:
Enter the value of x :2
Enter the value of n: 5
sum of first 5 terms: 63.0
Result:
Hence, the sum of the finite geometric series and verified with sample inputs.
EXPT NO: 4 CHECKING PALINDROME NUMBER OR NOT.
Aim:
To write a python program to check whether the given number is palindrome or not.
Algorithm:
Start the program.
Input Value:
o Get an integer num from the user.
Initialize Variables:
o Set rev to 0. This will be used to build the reversed number.
o Set wnum to num. This will be used to process the original number.
Reverse the Number:
o While wnum is greater than 0:
o Extract the last digit of wnum using wnum % 10.
o Append this digit to rev by multiplying rev by 10 and adding the digit.
o Remove the last digit from wnum by performing integer division wnum // 10.
Check Palindrome:
o Compare the reversed number rev with the original number num.
o If they are equal, print that the number is a palindrome.
o Otherwise, print that the number is not a palindrome.
Stop the program
Program:
num=int(input("Enter the number to be checked :"))
rev=0
wnum=num
Output:
while wnum>0: Enter the number to be checked: 121
The given number 121 is palindrome
digit=wnum%10
rev=rev*10+digit Enter the number to be checked: 123
The given number 123 is not a palindrome
wnum=wnum//10
if rev==num:
print("The given number",num," is palindrome")
else:
print("The given number",num," is not a palindrome")
Result:
Hence, the Python program to check whether a given number is a palindrome was successfully
written and executed. The program works correctly for various test cases.
EXPT NO: 5 CALCULATE THE FACTORIAL OF A NUMBER.
Aim:
To write a python program to calculate the factorial of a number.
Algorithm:
Start the program.
Input:
o Read an integer value num from the user.
Initialize:
o Set fact to 1 (to hold the result of the factorial calculation).
o Set a to 1 (to use as a counter for multiplication).
Process:
o While a is less than or equal to num:
o Update fact to be the product of fact and a (i.e., fact = fact * a).
o Increment a by 1 (i.e., a = a + 1).
Output:
o Print the result showing the factorial of the given number num.
Stop the program
Program:
num=int(input("Enter the number :"))
fact=1
a=1
while a<=num:
fact=fact*a
a=a+1
print("The factorial of the given number",num,"is",fact)
Output:
Enter the number: 5
The factorial of the given number 5 is 120
Result:
Hence, the Python program to calculate the factorial of a number was successfully written and
executed. It handles both positive and negative inputs correctly.
EXPT NO: 6 SEARCH FOR AN ELEMENT IN A GIVEN LIST
Aim:
To write a python program to search for an element in a given list of numbers.
Algorithm:
Start the program.
Input the List:
o Prompt the user to enter a list.
Determine the Length:
o Calculate the length of the list using length = len(lst).
Input the Element to Search:
o Prompt the user to enter the element they want to search for .
Initialize Search:
o Loop through the list using a for loop from 0 to length – 1.
Check Each Element:
o For each index i, check if element is equal to lst[i].
o If they are equal:
Print that the element was found and its index
Break the loop to stop further searching.
Handle Element Not Found:
o If the loop completes without finding the element, execute the else block associated with
the loop.
o Print that the element was not found in the list.
Stop the program.
Program:
lst = eval(input("Enter list:"))
length= len(lst)
element= int(input("Enter the elements to be searched for:"))
for i in range(0,length):
if element == lst[i]: Output:
print(element,"found at index",i) Enter list: [2,8,9,11,-55,-11,22,78,67]
break Enter the elements to be searched for:22
else: 22 found at index 6
print(element,"not found in given list")
Result:
Hence, the Python program to search for an element in a list was successfully written and
executed. The program works correctly for both present and absent values.
EXPT NO: 7 DISPLAY THE NAMES OF THE STUDENTS WHO HAVE SCORED MARKS
ABOVE 75 USING DICTIONARIES
Aim:
To write a python program to create a dictionary with the roll number, name and marks of n
students in a class and display the names of students who have marks above 75.
Algorithm:
Start the program.
Input: Ask the user for the number of students, n.
Initialize Dictionary: Create an empty dictionary stu to store student details.
Loop: For i from 1 to n:
o Prompt the user to enter the details for each student (roll number, name, marks).
o Store the student details (roll number, name, and marks) in a dictionary d.
o Create a unique key (e.g., "stu1", "stu2", etc.) and add the student details to the stu
dictionary using this key.
Display Students with Marks > 75:
o Print the heading "Student with mark > 75 are".
o Loop through all students in the stu dictionary:
For each student, check if their marks are greater than or equal to 75.
If yes, print the student's details.
Stop the program
Program:
n=int(input("How many students?"))
stu={}
for i in range(1,n+1):
print("Enter the details of Student",(i))
rollno= int(input("Enter the Roll no:"))
name=input("Enter Name:")
marks=float(input("Marks:"))
d={"Roll_no:" : rollno,"name": name, "Marks" : marks}
key="stu" + str(i)
stu[key]=d
print("Student with mark > 75 are")
for i in range(1,n+1):
key="stu" + str(i)
if stu[key]["Marks"] >=75:
print(stu[key])
Output:
How many students?4
Enter the details of Student 1
Enter the Roll no:101
Enter Name:Rathi
Marks:78
Enter the details of Student 2
Enter the Roll no:102
Enter Name:Bala
Marks:98
Enter the details of Student 3
Enter the Roll no:103
Enter Name:Zulfy
Marks:65
Enter the details of Student 4
Enter the Roll no:104
Enter Name:Sara
Marks:78
Student with mark > 75 are
{'Roll_no:': 101, 'name': 'Rathi', 'Marks': 78.0}
{'Roll_no:': 102, 'name': 'Bala', 'Marks': 98.0}
{'Roll_no:': 104, 'name': 'Sara', 'Marks': 78.0}
Result:
Hence, the Python program to display the names of students who scored above 75 using
dictionaries was successfully written and executed.
EXPT NO: 8 CREATE A NEW DICTIONARY USING COMMON KEYS OF OTHER
DICTIONARY
Aim:
To write a python program to create a third dictionary from two dictionaries having some common
keys, in ways so that the values of common keys are added in the third dictionary.
Algorithm:
Start the program.
Initialize Dictionaries:
o Define dct1 and dct2 with key-value pairs.
Copy dct1:
o Create a copy of dct1 called dct3.
Update dct3:
o Update dct3 with the contents of dct2, adding all key-value pairs from dct2 to dct3.
Add Values for Common Keys:
o Loop through each key-value pair in dct1 and dct2:
If the key exists in both dictionaries (dct1 and dct2), add their corresponding values
and update dct3 with the sum.
Output:
o Print dct1, dct2, and the final dct3 after updating with the summed values.
Stop the program
Program:
dct1={'1':100,'2':200,'3':300}
dct2={'1':100,'2':200,'5':400}
dct3=dict(dct1)
dct3.update(dct2) Output:
for i,j in dct1.items(): Two given dictionaries are :
for x,y in dct2.items(): {'1': 100, '2': 200, '3': 300}
if i==x: {'1': 100, '2': 200, '5': 400}
dct3[i]=(j+y) The resultant dictionary
print("Two given dictionaries are : ") {'1': 200, '2': 400, '3': 300, '5': 400}
print(dct1)
print(dct2)
print("The resultant dictionary")
print(dct3)
Result:
Hence, the Python program to create a new dictionary using the common keys of two dictionaries
was successfully written and executed.