Practical File Class XI
Computer Science (083)
Program 1: Write a program to calculate the sum of two numbers and print their sum.
num1 = 1.5
num2 = 6.3
sum = num1 + num2
print(sum)
Output :
7.8
Program 2: Write a program that accepts the radius of a circle and prints its area.
radius = float(input("Input the radius of the circle : "))
area = (22/7)*radius*radius
print (area)
Output :
Input the radius of the circle : 7.0
154.0
Program 3:Write a program to compute simple interest.
P = float(input("Enter Principal: "))
R = float(input("Enter Rate of Interest (in % per annum) : "))
T = float(input("Enter Time Period : "))
# Calculates simple interest
SI = (P * R * T) / 100
print(SI)
Output :
Enter Principal: 1000
Enter Rate of Interest (in % per annum) : 20
Enter Time Period : 3
600.0
Program 4: Write a program to take sides of a triangle as input and print its area.
a = float(input("Enter first side: "))
b = float(input("Enter second side: "))
c = float(input("Enter third side: "))
# calculate the semi-perimeter
s = (a+b+c)/2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c))**0.5
print(area)
Output :
Enter first side: 5
Enter second side: 6
Enter third side: 7
14.696938456699069
Program 5:
Write a python Program to find the absolute value of the number entered by the user.
num = int(input("Enter any number : "))
if num>=0:
abs_num = num
else:
abs_num= -num
print(abs_num)
Output
Enter any number : -5
5
Program 6: Write a program to find whether a given number is even or odd?
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Output :
Enter a number: 95
Odd Number
Program 7:
Write a Python Program (using for loop) to Find the Sum of First N Natural Numbers
where N is entered by the user.
sum=0
n = int(input("Enter the value of n : "))
for num in range(1, n+1):
sum = sum + num
print ("Sum of Natural Numbers : ", sum)
Output:
Enter the value of n : 20
Sum of Natural Numbers : 210
Program 8: Write a program to find the factorial of a number.
num = int(input("Enter a number to find factorial: "))
factorial = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
else:
for i in range(2,num+1):
factorial = factorial*i
print("The factorial of is ",factorial)
Output :
Enter a number to find factorial: 6
The factorial of is 720
Program 9: Write a program to display the Fibonacci sequence up to n-th term where n
is provided by the user
first = 0
second = 1
counter = 0
number_of_terms = int(input("Enter Number of terms : "))
if number_of_terms <= 0:
print("Please enter a positive integer")
else:
while counter < number_of_terms:
print(first, end = " ")
temp = first + second
first = second
second = temp
counter = counter + 1
Output :
Enter Number of terms : 10
0 1 1 2 3 5 8 13 21 34
Program 10 Write a program in Python to check if a number entered by a user is a
prime number or not.
number = int(input("Enter a positive number greater than one : "))
flag = 0
if number > 1:
for i in range(2,number):
if ((number%i) == 0):
flag = 1
break
if (flag==1):
print("Not prime")
else:
print("Prime")
Output :
Enter a positive number greater than one : 79
Prime
Program 11 Write a Program to check if the input year is a leap year or not
year = int(input("Enter a year : "))
if(((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)):
print("Leap Year")
else:
print("Non Leap Year")
Output :
Enter a year : 2100
Non Leap Year
Program 12: Write a Python program to remove duplicates from a list.
L = [2, 4, 10, 20, 5, 2, 20, 4]
F = []
for n in L:
if n not in F:
F.append(n)
print(F)
Output :
[2, 4, 10, 20, 5]
Program 13: Write a Python program to count the number of strings of length 2 or
more and the first and last character are same from a given list of strings
words = ['abc', 'xyz', 'aba', '1221']
count=0
for w in words:
if len(w) > 1 and w[0] == w[-1]:
count = count + 1
print(count)
Output :
2
Program 14: Write a Python program to find the sum of all even numbers and all odd
numbers from the given tuple.
T = (8, 10, 13, 5, 6,9)
L= len(T)
even=0
odd=0
for i in range(L):
if T[i]%2==0:
even=even + T[i]
else:
odd = odd + T[i]
print("Sum of Even Numbers is = ", even)
print("Sum of Odd Numbers is = ", odd)
Output:
Sum of Even Numbers is = 24
Sum of Odd Numbers is = 27
Program 15: Write a program in Python, To input a list of numbers. Thereafter
increments all even numbers by 1 and decrements all odd numbers by 1.
L = [8, 10, 13, 5, 6, 9]
print("Original List = ", L)
for i in range(len(L)):
if L[i]%2==0:
L[i]=L[i] + 1
else:
L[i] = L[i] - 1
print("Updated List = ", L)
Output:
Original List = [8, 10, 13, 5, 6, 9]
Updated List = [9, 11, 12, 4, 7, 8]
Program-16: Write a program in Python to count the frequency of all elements of a
tuple.
T= (10, 20, 30, 20, 40, 30, 20, 50)
print("Tuple is: ",T)
Uni= ()
for i in T:
if i not in Uni:
Uni = Uni + (i,)
for i in Uni:
print(i,":",T.count(i))
Output:
Tuple is: (10, 20, 30, 20, 40, 30, 20, 50)
10 : 1
20 : 3
30 : 2
40 : 1
50 : 1
Program-17: Write a Program in Python to count the number of times a character
appears in a given string using a dictionary.
S=input("Enter a string: ")
D={}
for c in S:
if c in D:
D[c]+=1
else:
D[c]=1
print("Frequency of Characters in String: \n",D)
Output:
Enter a string: S P SHARMA
Frequency of Characters in String:
{'S': 2, ' ': 2, 'P': 1, 'H': 1, 'A': 2, 'R': 1, 'M': 1}