0% found this document useful (0 votes)
23 views15 pages

CS Practicals - 250907 - 155336

Class 12 cs practical
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
23 views15 pages

CS Practicals - 250907 - 155336

Class 12 cs practical
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 15
(LASS 12TH COMPUTER SCIENCE PRACTICALS 1. Write a program to find whether a given number is odd or even. 2. Write a program to find the factorial of a number. (Do these without using function) 3. Write a program to find the sum of n natural numbers 4. Write a program to print Fibonacci series of n numbers 5. Two lists Lname and Lage contain name of person and age of person respectively. A list named Lnameage is empty. Write functions as per details given below: (i) Push_na(): It will push the tuple containing pair of name and age from Lname and Lage whose age is above 50. (ii) Pop_na(): It will remove the last pair of name and age and also print name and age of the removed person. It should also print "underflow" if there is nothing to remove. Example. Lname = ['Narender', ‘Jaya', 'Raju', ‘Ramesh’, ‘Amit', 'Piyush'] Lage = [45,23,59,34,51,43] After Push_na(): [('Raju', 59), (‘Amit', 51)] Pop_na(): The name removed is Amit, The age of person is 51 6. Adictionary stu contains rollno and marks of students. Two empty lists stack_roll and stack_mark will be used as Stack. Two functions Push_stu() and Pop_stu() are defined and perform the following operation: (i) Push_stu(): It reads dictionary stu and adds keys into stack_roll and values into stack_mark for all students who secured more than 60 marks. (ii) Pop_stu(): It removes last rollno and marks from both lists and prints “underflow” if there is nothing to remove. Example: stu = {1:56, 2:45, 3:78, 4:65, 5:35, 6:90} After Push_stu(): stack_roll = [3,4,6], stack_mark = [78,65,90] 7. Consider a list named Nums which contains random integers. Write the following user-defined functions in Python and perform the specified operations on a Stack named BigNums. (i) PushBig(): It checks every number from the list Nums and pushes all such numbers which have 5 or more digits into the Stack, BigNums. (ii) PopBig(): It pops the numbers from the Stack BigNums and displays them. The function should also display "Stack Empty" when there are no more numbers left in the Stack. Example: Nums = [213,10025,167,254923, 14, 1297653, 31498, 386 ,92765] PushBig() ~ BigNums = [10025,254923, 1297653, 31498, 92765] PopBig() + 92765, 31498, 1297653, 254923, 10025, Stack Empty 8. Write a program to implement Push, Pop, Display in stack using list with a menu-driven method. 9. Write a program to read and display file content line by line with each word separated by #. 10. Write a program for Emp.dat (e-id, e-name, Dept, Salary) (i) Tadd() — Input employee data and append in a binary file. (ii) Tupdate() - Update the salary of employees in the IT department to %2,00,000. Example data, copy code 150000) 0) 125000) Question 1: Write a program to find a given number is odd or even using function. Python def check_even_odd(num): if num % 2 == 0: print(num, "is Even") else: print(num, "is Odd") n = int(input("Enter a number: ")) check_even_odd(n) Sample Output: Enter a number: 12 12 is Even Question 2 (a): Write a program to find factorial of a number using function (for loop). Python def factorial(n): fact = 1 for i in range(1, n+1): fact *= i return fact num = int(input("Enter a number: ")) print("Factorial of", num, "is", factorial(num) ) Sample Output: Enter a number: 5 Factorial of 5 is 120 Question 2 (b): Write a program to find factorial of anumber using function (while loop). Python def factorial(n) fact = 1 i=l while i <= n: fact *= i i+=1 return fact num = int(input("Enter a number: ")) print("Factorial of", num, "is", factorial(num) ) Sample Output: Enter a number: 6 Factorial of 6 is 720 Question 3: Write a program to find the sum of n natural numbers. Python n = int(input("Enter n: ")) if n <= 0: print("Invalid input! n must be a positive natural number.") elise s=0 for i in range(1, n+1): Sete print("Sum of", n, “natural numbers is:*, s} Sample Output 1 (valid input): Enter n: 10 Sum of 10 natural numbers is: 55 Sample Output 2 (invalid input): Enter n: -3 Invalid input! n must be a positive natural number. 4. Program to print Fibonacci series Python n = int(input("Enter number of terms: ")) a, b=0, 1 print("Fibonacci Series:") for i in range(n): print(a, end=" ") a, b = b, atb Output: Enter number of terms: 7 Fibonacci Series: OI 1s 235 58 5. List of Names & Ages with Push and Pop Python Lname = ['Narender', ‘Jaya', ‘Raju', "Ramesh', ‘Amit', ‘Piyush'] Lage = [45, 23, 59, 34, 51, 43] Lnameage = [] def Push_na(): for i in range(len(Lname)): if Lage[i] > 50: Lnameage .append((Lname[i], Lage[i])) print("Stack after Push:", Lnameage) def Pop_na(): if len(Lnameage) == 0: print( "Under flow") elise: name, age = Lnameage.pop() print("The name removed is", name) print("The age of person is", age) # Example Run Push_na() Pop_na() Output: Stack after Push: [('Raju', 59), (‘Amit', 51)] The name removed is Amit The age of person is 51 6. Dictionary stu with stack push/pop Python stu = {1:56, 2:45, 3:78, 4:65, 5:35, 6:90} stack_roll = [] stack_mark t] def Push_stu(): for r, m in stu.items(): if m > 60: stack_roll.append(r) stack_mark.append(m) print("Stack Roll:", stack_roll) print("Stack Marks:", stack_mark) def Pop_stu(): if len(stack_roll) == 0: print("Under flow") ‘else: r = stack_roll.pop() m = stack_mark.pop() print("Removed Roll:", r, " Marks:" m) # Example Run Push_stu() Pop_stu() Output: Stack Roll: [3, 4, 6] Stack Marks: [78, 65, 90] Removed Roll: 6 Marks: 90 7. PushBig and PopBig Python Nums = [213,10025,167,254923,14,1297653,31498,386,9 2765] BigNums = [] def PushBig(): for n in Nums: if len(str(n)) >= 5: BigNums.append(n) print("BigNums Stack:", BigNums) def PopBig(): while BigNums: print(BigNums.pop()) print("Stack Empty") # Example Run PushBig() PopBig() Output: BigNums Stack: [10025, 254923, 1297653, 31498, 92765] 92765 31498 1297653 254923 10025 Stack Empty Question 8: Write a program to implement Push, Pop, and Display in stack using list with menu-driven method. Python stack = [] def push(): def def item = input("Enter element to push: ") stack. append(item) print("Stack:", stack) pop(): if not stack: print(“Underflow") else print("Popped:", stack.pop()) display(): if not stack print("Stack is empty") else print("Stack:", stack) while True: print("\n1. Push 2. Pop 3. Display 4. Exit") ch = int(input("Enter choice: ")) che push() elif ch == 2: pop() elif ch == 3: display() elif ch == 4: print("Exiting program...") break else print("Invalid Choice") Sample Output (Execution Flow) 1. Push 2. Pop 3. Display 4. Exit Enter choice: 1 Enter element to push: A Stack: ['A'] 1. Push 2. Pop 3. Display 4. Exit Enter choice: 1 Enter element to push: B Stack: ['A', ‘B"] 1. Push 2. Pop 3. Display 4. Exit Enter choice: 3 Stack: ['A B'] 1. Push 2. Pop 3. Display 4. Exit Enter choice: 2 Popped: B 1. Push 2. Pop 3. Display 4. Exit Enter choice: 3 Stack: ['A'] 1. Push 2. Pop 3. Display 4. Exit Enter choice: 2 Popped: A 1. Push 2. Pop 3. Display 4. Exit Enter choice: 2 Under flow 1. Push 2. Pop 3. Display 4. Exit Enter choice: 4 Exiting program... 9. File read with # separator Python with open("sample.txt","r") as f: for line in f: words = line.strip().split() print("#".join(words)) If file contains: Hello World Python is fun Output: Hello#wWorld Python#is#fun Question 10: Write a program for Emp.dat (e-id, e-name, Dept, Salary). (i) Tadd() - input employee data and append in a binary file. (ii) Tupdate() — update the salary of employees in the IT department to %2,00,000. Program Python import pickle def Tadd(): f = open("Emp.dat","ab") n = int(input("Enter number of employees: ")) for i in range(n): e_id = int(input("Enter Emp ID: ")) e_name = input("Enter Emp Name Dept = input("Enter Department: ") Salary = int(input("Enter Salary: ")) rec = [e_id, e_name, Dept, Salary] pickle.dump(rec, f) f.close() de Tupdate(): f = open("Emp.dat","rb") records = [] try while True: rec = pickle. load(f) if rec[2] IT": # check departmen rec[3] = 200000 # update salary records. append(rec) except EOFError: f.close() f = open("Emp.dat", "wb") for rec in records: pickle.dump(rec, f) f.close() print("Updated IT Department salaries.") Example Run (Data given in question Tadd() -- 4 # Tupdate() --> upda Initial Employee Records Added (using Tadd): 101, Rajesh, Personal, 1000000 102, Sanjeev, HRD, 150000 103, John, IT, 75000 104, Manoj, Accounts, 125000 105, Sunil, Production, 190000 After running Tupdate(): Updated IT Department salaries. Final Records inside Emp.dat: 101, Rajesh, Personal, 1000000 102, Sanjeev, HRD, 150000 103, John, IT, 200000 < 104, Manoj, Accounts, 125000 105, Sunil, Production, 190000

You might also like