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/ 17
COMPUTER SCIENCE PRACTICAL FILE
Q1.) Write a program to input a Welcome message
and print it. Message: Welcome to computer science with python. Ans.) a = str(input("Enter a welcome message : ")) print(a)
Q2.) Write a program to add three numbers.
Ans.) a = int(input("Enter a number")) b = int(input("Enter a number")) c = int(input("Enter a number")) print("Sum = ", a+b+c) Q3.) Write a program to find out the length and breadth of a rectangle and also, calculate its area. Ans.) l = float(input("Enter the length : ")) b = float(input("Enter the breadth : ")) print("The area of the rectangle is ", l*b)
Q4.) Write a program to find out exponent of 4.
Ans.) c = float(input("Enter the exponent : ")) d = 4**c print("4 raised to the power of ", c, "is ", d) Q5.) Write a program to input two numbers and swap them. Ans.) num1 = float(input("Enter the first number : ")) num2 = float(input("Enter the second number : ")) print(num1, num2) num1, num2 = num2, num1 print(num1, num2)
Q6.) Write a program to find out simple interest.
Ans.) P = float(input("Enter the Principal Amount : ")) R = float(input("Enter the Rate of Interest : ")) T = float(input("Enter the Time Period : ")) print("The Simple Interest is ", P*R*T/100) Q7.) Write a program to calculate percentage of a student (5 subjects). Ans.) sub1 = int(input("Enter your marks in Subject 1 : ")) sub2 = int(input("Enter your marks in Subject 2 : ")) sub3 = int(input("Enter your marks in Subject 3 : ")) sub4 = int(input("Enter your marks in Subject 4 : ")) sub5 = int(input("Enter your marks in Subject 5 : ")) subsum = sub1+sub2+sub3+sub4+sub5 percentage = subsum/5 print("Your percentage is ", percentage, "%") Q8.) Write a program to calculate the salary of a person. Ans.) basic = int(input("Enter your basic salary : ")) da=(15*basic)/100 hr=(10*basic)/100 daonta=(3*basic)/100 gross =basic + da + hr + daonta print("Your gross salary is ", gross)
Q9.) Write a program to print the square of the given
number if it is even otherwise print its cube. Ans.) a = int(input("Enter a number : ")) if a/2-a//2 == 0: print("The square of the given even no. is ", a**2) else: print("The cube of the given odd no. is ", a**3)
Q10.) Write a program to take a number from the
user and print the factorial of that number. Ans.) num = int(input("Enter a number: ")) factorial = 1 if num < 0: print("Factorial does not exist for negative numbers") elif num == 0: print("The factorial of 0 is 1") else: for i in range(1,num + 1): factorial = factorial*i print("The factorial of",num,"is",factorial) Q11.) Write a program to take a number from the user and print the sum of all the numbers from 1 till the given number. Ans.) num = int(input("Enter a number: ")) sumall = 0 for i in range(1,num + 1): sumall = sumall+i print("The sum of all numbers from 1 to ", num," is ", sumall)
Q12.) Write a program to print the square of each
number from 1 till n, which is given by the user. Ans.) n= int(input("Enter a number : ")) o=1 while n != o-1: print(o**2) o = o+1
Q13.) Write a program to take two numbers from the
user and print the odd numbers between the given range. Ans.) start = int(input("Enter the start of the range: ")) end = int(input("Enter the end of the range: ")) print("Odd numbers between", start, "and", end, "are:") for num in range(start, end + 1): if num % 2 != 0: print(num) Q14.) Write a program to take a number from the user to print first 10 multiples of a given number. Ans.) a1 = int(input("Enter a number : ")) b=1 while b != 11: print(a1*b) b = b+1 Q15.) Write a program to take one number from the user and check whether it’s a palindrome. Ans.) n=int(input("Enter number:")) temp=n rev=0 while n>= 1: dig=n%10 rev=rev*10+dig n=n//10 if temp==rev: print("The number is a palindrome!") else: print("The number isn't a palindrome!") Q16.) Write a program to print the sum of digits of a given number. Ans.) n=int(input("Enter a number:")) total=0 while(n>0): dig=n%10 total=total+dig n=n//10 print("The total sum of digits is:",total)
Q17.) Write a program to generate a Fibonacci
series of 10 elements. Ans.) n1, n2 = 0, 1 count = 0 n_terms = 10 if n_terms <= 0: print("Please enter a positive integer") elif n_terms == 1: print("Fibonacci sequence up to", n_terms, ":") print(n1) else: print("Fibonacci sequence:") while count < n_terms: print(n1, end=" ") nth = n1 + n2 n1 = n2 n2 = nth count += 1
Q18.) Write a program to print the following series:
Ans.) A) a = int(input("Enter first term: ")) r = int(input("Enter the common ratio: ")) n = int(input("Enter the no. of terms: ")) m=1 while m<=n: p = a * r**(m-1) print(p) m += 1
B) a = int(input("Enter first term : "))
r = int(input("Enter the common ratio : ")) n = int(input("Enter the no. of terms : ")) m=1 while m<=n: p = a * r**(m-1) print(1/p) m += 1
Q19.) Write a program to find the grade of a student
when grades are allocated as given below. Get marks in 5 subjects, find grade accordingly - a)Above 90% =A b) 80% to 90%=B c)70% to 80%=C d) 60% to 70%=D e)Below 60%=E Ans.) sub1 = int(input("Enter your marks in Subject 1 : ")) sub2 = int(input("Enter your marks in Subject 2 : ")) sub3 = int(input("Enter your marks in Subject 3 : ")) sub4 = int(input("Enter your marks in Subject 4 : ")) sub5 = int(input("Enter your marks in Subject 5 : ")) subsum = sub1+sub2+sub3+sub4+sub5 percentage = subsum/5 print("Your percentage is ", percentage, "%") if percentage >= 90: print("Your grade is A.") elif percentage >= 80: print("Your grade is B.") elif percentage >= 70: print("Your grade is C.") elif percentage >= 60: print("Your grade is D.") else: print("Your grade is E.")
Q20.) Write a program to print the following pattern.
Ans.) a) n = int(input("Enter number of rows: ")) for i in range(1,n+1): for j in range(1, i+1): print(j, end="") print()
b) n = int(input("Enter number of rows: "))
for i in range(1,n+1): for j in range(1, i+1): print(i, end="") print("")