CS_practical_file[1]
CS_practical_file[1]
Sum=a+b
2. Write a program that accepts the radius of a circle and prints its
area.
# 2.Write a program that accepts radius of a circle and prints its area.
r=int(input('Enter the radius of circle:'))
Area=3.14*r**2
print('The area of the circle is:', Area)
3. Write a program that accepts base and height and calculate the
area of a triangle.
#Write a program that accepts base and height and calculate the area of triangle
Area=(1/2)*b*h
1/25
print('Enter the marks of three subject out of 100')
a=float(input('Enter the marks of first subject:'))
b=float(input('Enter the marks of second subject:'))
c=float(input('Enter the marks of third subject:'))
P=(a+b+c)/3
print('The percentage marks are:', P,'%')
A=a**2
T=((3**0.5)/4)*a**2
SI=(P*R*T)/100
# Write a program to read two numbers and prints their quotient and reminder
a=float(input('Enter the dividend:'))
b=float(input('Enter the divisor:'))
Q=a//b
R=a%b
2/25
8. Write a program to find whether a given number is even or odd.
10. Write a program to find the lowest among the three integers.
11. Write a program that accepts the length and breadth of the
rectangle and calculate its area.
3/25
# Write a program to that accepts length and
# breadth of rectangle and calculate its area.
print('Rectangle Specifications')
print('Length=',l)
print('Breadth=', b)
print('Area=', area)
13. Write a program that reads the number n and prints the value
of n², n³ and n⁴.
4/25
# Write a program to accept the marks of five subjects
# and calculate the average marks.
Feet=a*0.032
Inch=a*0.393
16. Write a program that accepts the age and print if one is eligible
to vote or not.
17. Write a program that accepts two numbers and check if the first number is fully
divisible by the second number or not.
5/25
# Write a program that accepts two numbers and
# check if the first number is fully divisible by second number or not.
Area=b*h
Perimeter=2*(b+w)
19. Write a program to accept the year and check if it is a leap year or not.
6/25
import math
print('To calculate 4x⁴+3y³+9z+6π')
x=float(input('Enter the number x:'))
y=float(input('Enter the number y:'))
z=float(input('Enter the number z:'))
b=(4*math.pow(x,4))+(3*math.pow(y,3))+(9*z)+(6*math.pi)
print('The result of the above expression is:',b)
import math
a=math.pow(x,2)
b=math.sqrt(x)
if x%2!=0:
print('The value of square is:',a)
else:
print('The value of square root is:',b)
if a>=0:
if a==0:
print('The number is zero')
else:
print('The number is a positive number')
else:
print('The number is a negative number')
7/25
23. Write a program to input percentage marks of a student and
find the grade as per the following criterion:
'''
Write a program to input percentage marks of a student and find the grade as per
following criterion:
Marks Grade
>=90 A
75-90 B
60-75 C
Below 60 D
'''
a=float(input('Enter the percentage marks:'))
if a>=90:
print('The student has got an A grade')
elif a>=75 and a<90:
print('The student has got a B grade')
elif a>=60 and a<75:
print('The student has got a C grade')
else:
print('The student has got a D grade')
8/25
r=float(input('Enter the radius of the circle:'))
print('1.Calculate perimeter')
print('2.Calculate area')
choice=int(input('Enter your choice (1 or 2):'))
if choice==1:
peri=2*3.14159*r
print('Perimeter of the circle with radius',r,':',peri)
else:
area=3.14159*r*r
print('Area of the circle of the radius',r,':',area)
9/25
print('You have entered a digit.')
else:
print('You have entered a special character.')
28. Write a program to calculate and print the roots of a quadratic equation ax²+bx+c=0.
(a≠0)
import math
print('For quadratic equation, ax²+bx+c=0,enter coefficents below')
a=int(input('Enter a:'))
b=int(input('Enter b:'))
c=int(input('Enter c:'))
if a==0:
print('Value of a should not be zero')
print('Aborting!!')
else:
d=b*b-4*a*c
if d>0:
root1=(-b+math.sqrt(d))/(2*a)
root2=(-b-math.sqrt(d))/(2*a)
print('Roots are real and unequal')
print('Root1=',root1,',Root2=',root2)
elif d==0:
root1=-b/2*a
print('Roots are real and equal')
print('Root1=',root1,',Root2=',root1)
else:
print('Roots are complex and imaginary')
29. Write a program to print the sum of natural numbers between 1 to 7. Print the sum
progressively i.e. after adding each natural number, a print sum so far.
Sum=0
for n in range(1,21):
Sum+=n
10/25
print('Sum of natural numbers <=',n,'is',Sum)
num=int(input('Enter a number:'))
fact=1
a=1
while a<=num:
fact*=a
a+=1
print('The factorial of',num,'is',fact)
for i in range(1,6):
print()
for j in range(1,i):
print('*',end=' ')
Download
# Write a Python script to print Fibonacci series’ first 10 elements.
first=0
second=1
print(first, end=' ')
print(second,end=' ')
for a in range(1,9):
third=first+second
print(third,end=' ')
first,second=second,third
11/25
num=int(input('Enter a number (>1000):'))
tnum=num
reverse=0
while tnum>0:
digit=tnum%10
reverse=reverse*10+digit
tnum=tnum//10
print('Reverse of',num,'is',reverse)
34. Input three angles and determine if they form a triangle or not.
# Input three angles and determine if they form a triangle or not.
angle1=angle2=angle3=0
angle1=float(input('Enter the first angle:'))
angle2=float(input('Enter the second angle:'))
angle3=float(input('Enter the third angle:'))
if angle1+angle2+angle3==180:
print('The angles form a triangle')
else:
print('The angles do not form a triangle')
35. Write a Python script that displays the first ten Mersenne numbers.
# Write a Python script that displays first ten Mersenne numbers.
print('First 10 Mersenne numbers are:')
for a in range(1,11):
mersnum=2**a-1
print(mersnum,end=' ')
print()
36. Write a Python script that displays the first ten Mersenne numbers and displays
‘Prime’ next to Mersenne Prime Numbers.
Download
# Write a Python script that displays first ten
# Mersenne numbers and displays ‘Prime’ next to Mersenne Prime Numbers.
for a in range(1,21):
mersnum=2**a-1
mid=mersnum//2+1
for b in range(2,mid):
if mersnum%b==0:
print(mersnum)
12/25
break
else:
print(mersnum,'\tPrime')
1 3
1 3 5
1 3 5 7
13/25
# Write python script to print the following pattern.
'''
1
13
135
1357
'''
for a in range(3,10,2):
print()
for b in range(1,a,2):
print(b, end=' ')
print()
40. Write a python script to input two numbers and print their LCM and HCF.
41. Write a python script to calculate the sum of the following series:
14/25
S=(1)+(1+2)+(1+2+3)+……+(1+2+3+….+n)
# Write a python script to calculate the
# sum of the following series: S=(1)+(1+2)+(1+2+3)+……+(1+2+3+….+n)
Sum=0
n=int(input('How many terms?'))
for a in range(2,n+2):
term=0
for b in range(1,a):
term+=b
print('Term',(a-1),':',term)
Sum+=term
print('Sum of',n,'terms is:',Sum)
42. Write a program to print the following using a single loop (no nested loops)
1 1
1 1 1
1 1 1 1
1 1 1 1 1
'''
42.Write a program to print the following using a single loop (no nested loops)
1
11
111
1111
11111
'''
n=1
for a in range(5):
print(n,end = ' ')
print()
n=n*10+1
15/25
43. Write a program to print a pattern like:
4321
432
43
4
'''
for i in range(4):
for j in range(4,i,-1):
print(j,end=' ')
else:
print()
44. A program that reads a line and prints its statistics like:
16/25
line=input('Enter a line:')
lowercount=uppercount=0
digitcount=alphacount=0
for a in line:
if a.islower():
lowercount+=1
elif a.isupper():
uppercount+=1
elif a.isdigit():
digitcount+=1
if a.isalpha():
alphacount+=1
45. Write a program that reads a line and a substring and displays the number of
occurrences of the given substring in the line.
line=input('Enter line:')
sub=input('Enter substring:')
length=len(line)
lensub=len(sub)
start=count=0
end=length
while True:
pos=line.find(sub,start,end)
if pos!=-1:
count+=1
start=pos+lensub
else:
break
if start>=length:
break
print('No. of occurences of',sub,':',count)
46. Write a program that takes a string with multiple words and then capitalizes the first
letter of each word and forms a new string out of it.
17/25
#Write a program that takes a string with multiple
#words and then capitalizes the first letter of each word
#and forms a new string out of it.
string=input('Enter a string:')
length=len(string)
a=0
end=length
string2=''
while a<length:
if a==0:
string2+=string[0].upper()
a+=1
elif(string[a]==' ' and string[a+1]!=' '):
string2+=string[a]
string2+=string[a+1].upper()
a+=2
elif (string[a]==',' and string[a+1]!=','):
string2+=string[a]
string2+=string[a+1].upper()
a+=2
else:
string2+=string[a]
a+=1
print('Original string:',string)
print('Capitalized words string:',string2)
47. Write a program that reads a string and checks whether it is a palindrome string 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.')
18/25
break
else:
print(stri
48.W rite a program that reads a string and displays the longest substring of the given
string having just the consonants.
string=input('Enter a string:')
length=len(string)
maxlength=0
maxsub=''
sub=''
lensub=0
for a in range(length):
if string[a] in 'aeiou' or string[a] in 'AEIOU':
if lensub>maxlength:
maxsub=sub
maxlength=lensub
sub=''
lensub=0
else:
sub+=string[a]
lensub=len(sub)
a+=1
print('Maximum length consonant substring is:',maxsub,end=' ')
print('with',maxlength,'characters')
49. Write a program that reads a string and then prints a string that capitalizes every other
letter in the string.
string=input('Enter a string:')
length=len(string)
print('Original string:',string)
string2=''
for a in range(0,length,2):
string2+=string[a]
19/25
if a<length-1:
string2+=string[a+1].upper()
print('Alternatively capitalized string:',string2)
50. Write a program that reads the email id of a person in the form of a string and ensures
that it belongs to domain @edupillar.com (Assumption: no invalid characters are there in
email-id)
if sub==domain:
if ledo!=lema:
print('It is valid email id')
else:
print('This is invalid email id- contains just the domain name')
else:
print('This email-d is either not valid or belongs to some other domain')
51. WAP to remove all odd numbers from the given list.
# WAP to remove all odd numbers from the given list.
20/25
L= [ 41, 6, 9, 13,4, 23]
m=max (L)
secmax=L[0]
for i in range(1, len(L) ) :
if L[i]>secmax and L[i]<m:
secmax=L[i]
print ('The second largest element is: ',secmax)
for i in L:
if i not in L2:
x=L.count(i)
L1.append(x)
L2.append(i)
for i in range(len(L1)):
print (L2[i],'\t\t\t',L1[i])
54. WAP in Python to find and display the sum of all the values which are ending with 3
from a list.
# WAP in Python to find and display the sum of all the values which are ending with 3
from a list.
L=[33,13,92,99,3,12]
sum=0
x=len (L)
for i in range(0,x):
if type (L [i])== int:
if L[i]%10==3:
sum+=L[i]
print (s
Download
21/25
# WAP to search an element from the list
L=eval(input("Enter the elements:"))
n=len(L)
flag =1
s = int(input("Enter the element to be searched:"))
for i in range(0,n-1):
if(L[i]==s):
flag=1
break;
else:
flag=0
if flag==1:
print("Element found")
else:
print("Elemnet not found")
57. Write a program to input the total number of sections and stream name in 11th class
and display all information on the output screen.
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()
22/25
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
59. 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):
23/25
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")
#create a tuple
tuplex = "l","e","a","r","n","p","y","t","h","o","n","4","c","b","s","e"
print("Tuple Before Removing an item")
print(tuplex)
#converting the tuple to list
listx = list(tuplex)
#use different ways to remove an item of the list
listx.remove("4")
#converting the tuple to list
tuplex = tuple(listx)
print("\nTuple After Removing an item '4'")
print(tuplex)
61. Write a program to input n numbers from the user. Store these
numbers in a tuple. Print the maximum, minimum, sum and mean
of number from this tuple.
24/25
for i in range(0,n):
num = int(input())
#it will assign numbers entered by user to tuple 'numbers'
numbers = numbers +(num,)
print('\nThe numbers in the tuple are:')
print(numbers)
print("\nThe maximum number is: ")
print(max(numbers))
print("The minimum number is: ")
print(min(numbers))
print("The sum of numbers is: ")
print(sum(numbers))
print("The mean of numbers is: ")
mean = sum(numbers)/(i+1)
print(mean)
ele=int(input("Enter the element to be searched: "))
if ele in numbers:
print("Element found")
else:
print("Element not found")
25/25