Computer Science Laboratory Reference (PU-1)
1. Write a program to swap two numbers using the third variable.
a=int(input("Enter a value\n"))
b=int(input(“Enter b value\n"))
print("a value before swapping=",a)
print("b value before swapping=",b)
temp=a
a=b
b=temp
print("\na value after swapping=",a)
print("b value after swapping=",b)
SAMPLE OUTPUT 1
Enter a value=58
Enter b value=45
a value before swapping= 58
b value before swapping= 45
a value after swapping= 45
b value after swapping= 58
SAMPLE OUTPUT 2
Enter a value=18
Enter b value=25
a value before swapping= 18
b value before swapping= 25
a value after swapping= 25
b value after swapping= 18
2. Write a program to enter two integers and perform all arithmetic operations on them.
a=int(input("Enter first number="))
b=int(input("Enter second number="))
add=a+b
sub=a-b
product=a*b
div=a/b
print("\nAddition of two integers=",add)
print("Subtraction of two integers=",sub)
print("Product of two integers=",product)
print("Division of two integers=",div)
SAMPLE OUTPUT 1
Enter first number=25
Enter second number=3
Addition of two integers= 28
Subtraction of two integers= 22
Product of two integers= 75
Division of two integers= 8.333333333333334
SAMPLE OUTPUT 2
Enter first number=35
Enter second number=5
Addition of two integers= 40
Subtraction of two integers= 30
Product of two integers= 175
Division of two integers= 7.0
3. Write a python program to accept length and width of a rectangle and compute its
perimeter and area.
l=int(input("Enter the length of the rectangle="))
w=int(input("Enter the width of the rectangle="))
area=(l*w)
perimeter=(2*(l+w))
print("\nArea of rectangle=",area)
print("Perimeter of rectangle=",perimeter)
SAMPLE OUTPUT 1
Enter the length of the rectangle=5
Enter the width of the rectangle=6
Area of rectangle= 30
Perimeter of rectangle= 22
SAMPLE OUTPUT 2
Enter the length of the rectangle=7
Enter the width of the rectangle=9
Area of rectangle= 63
Perimeter of rectangle= 32
4. Write a python program to calculate the amount payable if money has been lent on
simple interest. Principal or money lent=P, Rate of interest=R% per annum and time =T
years.
p=int(input("Enter principal amount="))
t=int(input("Enter time taken="))
r=int(input("Enter rate of interest="))
si=(p*t*r)/100
amt=si+p
print("\nSimple interest=",si)
print("Amount payable=",amt)
SAMPLE OUTPUT 1
Enter principal amount=1000
Enter time taken=12
Enter rate of interest=3
Simple interest= 360.0
Amount payable= 1360.0
SAMPLE OUTPUT 2
Enter principal amount=1500
Enter time taken=12
Enter rate of interest=5
Simple interest= 900.0
Amount payable= 2400.0
5. Write a python program to find largest among three numbers.
num1=int(input("Enter first number="))
num2=int(input("Enter second number="))
num3=int(input("Enter third number="))
largest=num1
if num2>num1:
largest=num2
if num3>num1:
largest=num3
print("\nLargest of three numbers=",largest)
SAMPLE OUTPUT 1
Enter first number=6
Enter second number=5
Enter third number=3
Largest of three numbers= 6
SAMPLE OUTPUT 2
Enter first number=5
Enter second number=7
Enter third number=1
Largest of three numbers= 7
6. Write a python program that takes the name and age of the user as input and displays
a message whether the user is eligible to apply for a driving license or not.
name=(input("Enter the name of user=")
age=int(input("Enter the age of the user="))
if age>=18:
print(“\n”,name,"is eligible to apply for a driving license")
else:
print(“\n”,name,"is not eligible to apply for a driving license")
SAMPLE OUTPUT 1
Enter the name of user=Neha
Enter the age of the user=19
Neha is eligible to apply for a driving license
SAMPLE OUTPUT 2
Enter the name of user=Asha
Enter the age of the user=12
Asha is not eligible to apply for a driving license
7. Write a python program that prints minimum and maximum of five numbers entered
by the user.
number=[int(input("Enter a numbers="))for i in range(5)]
large=number[1]
small=number[1]
for i in number:
if i < small:
small=i
if i >large:
large=i
print("\nMaximum of five numbers=",large,"\n Minimum of five numbers=",small)
SAMPLE OUTPUT 1
Enter a numbers=5
Enter a numbers=12
Enter a numbers=63
Enter a numbers=88
Enter a numbers=120
Maximum of five numbers= 120
Minimum of five numbers= 5
SAMPLE OUTPUT 2
Enter a numbers=20
Enter a numbers=25
Enter a numbers=10
Enter a numbers=5
Enter a numbers=250
Maximum of five numbers= 250
Minimum of five numbers= 5
8. Write a python program to find the grade of a student when grade are allocated as
given in the table below.
Percentage of marks grade.
Above 90% A
80% to 90% B
70% to 80% C
60% to 80% D
Below 60% E
Percentage of the marks obtained by the student is input to the program.
total=0
for marks in range(6):
total+=float(input("Enter subject marks="))
print("\nYour total marks=",total)
per=total/6.0
print("Your percentage=",per)
if(per >=90 and per <=100):
print(" You secured A grade")
elif(per>=80 and per<90):
print("You secured B grade")
elif(per>=70 and per<80):
print("You secured C grade")
elif(per>=60 and per<70):
print("You secured D grade")
else:
print("You secured E grade!!!")
SAMPLE OUTPUT 1
Enter subject marks=90
Enter subject marks=92
Enter subject marks=99
Enter subject marks=95
Enter subject marks=89
Enter subject marks=95
Your total marks= 560.0
Your percentage= 93.33333333333333
You secured A grade
SAMPLE OUTPUT 2
Enter subject marks=67
Enter subject marks=45
Enter subject marks=71
Enter subject marks=56
Enter subject marks=10
Enter subject marks=90
Your total marks= 339.0
Your percentage= 56.5
You secured E grade!!!
9. Write a python program to print the table of a guven number. The number has to be
entered by the user.
num=int(input("Enter a number="))
for i in range(1,11):
print(num,'x',i,'=',num*i)
SAMPLE OUTPUT 1
Enter a number=20
20 x 1 = 20
20 x 2 = 40
20 x 3 = 60
20 x 4 = 80
20 x 5 = 100
20 x 6 = 120
20 x 7 = 140
20 x 8 = 160
20 x 9 = 180
20 x 10 = 200
SAMPLE OUTPUT 2
Enter a number=12
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
10. Write a python program to find the sum of an integer number. Input by the user.
num=int(input("Enter a number="))
sum=0
while num!=0:
rem=num%10
sum=sum+rem
num=num//10
print("Sum of a given number=",sum)
SAMPLE OUTPUT 1
Enter a number=657
Sum of a given number= 18
SAMPLE OUTPUT 2
Enter a number=73547
Sum of a given number= 26
11. Write a python program to check whether an input number is a palindrome or not.
num=int(input("Enter a number="))
rev=0
n=num
while num!=0:
rem=num%10
rev=rev*10+rem
num=num//10
if n==rev:
print(n,"is a palindrome number")
else:
print(n," is not a palindrome number")
SAMPLE OUTPUT 1
Enter a number=12321
2321 is a palindrome number
SAMPLE OUTPUT 2
Enter a number=7654
7654 is not a palindrome number
12. Write a python program to print the following patterns.
1 2345
1 234
1 23
1 2
1
rows = int(input("Enter the number of rows="))
for i in range(rows):
for j in range(1, rows-i+1):
print(j, end=' ')
print()
SAMPLE OUTPUT 1
Enter the number of rows=5
12345
1234
123
12
1
SAMPLE OUTPUT 2
Enter the number of rows=3
123
12
1
13. Write a python program that uses a user defined function that accepts name and
gender(as M for male and f for female) and prefixes Mr./Ms. Based on the gender.
def prefix(name,gender): # function definition
if (gender == 'M' or gender == 'm'):
print("Hello, Mr.",name)
elif (gender == 'F' or gender == 'f'):
print("Hello, Ms.",name)
else:
print("Please enter only M or F in gender")
name = input("Enter your name=")
gender = input("Enter your gender: M for Male, and F for Female=")
prefix(name,gender) # function call
SAMPLE OUTPUT 1
Enter your name=Arun
Enter your gender: M for Male, and F for Female=m
Hello, Mr. Arun
SAMPLE OUTPUT 2
Enter your name=Nisha
Enter your gender: M for Male, and F for Female=f
Hello, Ms. Nisha
14. Write a python program that has a user defined function to accept the coefficients of a
quadratic equation in variable a, b, c then calculate discriminant as b2-4ac. Write the
appropriate condition to check discriminant on positive, zero and negative and output
appropriate result.
import math
def equationroots( a, b, c): # function definition
dis = b*b-4*a*c
sqrt_val = math.sqrt(abs(dis))
if dis > 0:
print("Real and different roots")
print((-b + sqrt_val)/(2 * a))
print((-b - sqrt_val)/(2 * a))
elif dis == 0:
print("Real and same roots=",(-b / (2 * a)))
else:
print("Complex Roots")
print((- b+sqrt_val) / (2 * a))
print((- b-sqrt_val) / (2 * a))
a =int(input("Enter a value="))
b =int(input("Enter b value="))
c =int(input("Enter c value="))
if a == 0:
print("Input correct quadratic equation")
else:
equationroots(a, b, c) # function call
SAMPLE OUTPUT 1
Enter a value=-8
Enter b value=-1
Enter c value=0
Real and different roots
-0.125
-0.0
SAMPLE OUTPUT 2
Enter a value=1
Enter b value=-2
Enter c value=1
Real and same roots= 1.0
SAMPLE OUTPUT 3
Enter a value=2
Enter b value=-2
Enter c value=1
Complex Roots
1.0
0.0
SAMPLE OUTPUT 4
Enter a value=0
Enter b value=1
Enter c value=2
Input correct quadratic equation
15. Write a python program that has a user defined function to accept 2 numbers as
parameters, if number 1 is less than number 2 then numbers are swapped and returned.
def swap(a, b): # function definition
if a<b:
print("a value before swapping=",a)
print("b value before swapping=",b)
temp=a
a=b
b=temp
print("\na value after swapping=",a)
print("b value after swapping=",b)
elif a>b:
print("First value is larger than second value")
else:
print("Both are equal")
a=int(input("Enter a value="))
b=int(input("Enter b value="))
swap(a,b) # function call
SAMPLE OUTPUT 1
Enter a value=25
Enter b value=50
a value before swapping= 25
b value before swapping= 50
a value after swapping= 50
b value after swapping= 25
SAMPLE OUTPUT 2
Enter a value=3
Enter b value=2
First value is larger than second value
SAMPLE OUTPUT 3
Enter a value=2
Enter b value=2
Both are equal
PART B
16. Write a python program to input line(s) of text from the user until enter is processed,
Count the total number of characters in the text(including white spaces), total number of
alphabets, total numbers of digits and total number of special symbols and total number of
words in the given text.
entence_length=len(sentence)
print("\nLength of the string=", sentence_length)
t_alpha=t_digit=t_schars=0
for i in sentence:
if i.isalpha():
t_alpha+=1
elif i.isdigit():
t_digit+=1
else:
t_schars+=1
print("Total numbers of characters=", t_alpha)
print("Total number of digits=", t_digit)
print("Total number of special characters=",t_schars)
totalspace=0
for j in sentence:
if j.isspace():
totalspace+=1
print("Total number of words in the given sentence=", (totalspace+1))
SAMPLE OUTPUT 1
Write a sentence=Deeksha CFL Pu college@2024
Length of the string= 27
Total numbers of characters= 19
Total number of digits= 4
Total number of special characters= 4
Total number of words in the given sentence= 4
SAMPLE OUTPUT 2
Write a sentence=Hello children
Length of the string= 14
Total numbers of characters= 13
Total number of digits= 0
Total number of special characters= 1
Total number of words in the given sentence= 2
17. Write a python program to convert a string with more than one word into title case
string where strong is passed as parameter using functions.
def convertToTitle(string):
titleString = string.title()
print("The input string in title case is=",titleString)
userInput = input("Write a sentence= ")
totalSpace = 0 # count the number of spaces
for b in userInput:
if b.isspace():
totalSpace += 1
if(userInput.istitle()): # if a string is already title case
print("The String is already in title case")
elif(totalSpace > 0): # converting into title case
convertToTitle(userInput)
else:
print("The String is of one word only")
SAMPLE OUTPUT 1
Write a sentence= Deeksha
The String is already in title case
SAMPLE OUTPUT 2
Write a sentence= deeksha
The String is of one word only
SAMPLE OUTPUT 3
Write a sentence= deeksha cfl pu college
The input string in title case is= Deeksha Cfl Pu College