COMPUTER SCIENCE
PRACTICAL FILE
NAME: MAYANK
ROLL NO :
1. WAP in Python to calculate square root of a number.
num = float(input(“Enter a
number: “)) num_sqrt =
num**0.5
print(‘The square root of’, num, ‘is:’,num_sqrt)
OUTPUT
enter a number: 25
The square root of 25.0 is: 5.0
1. WAP in Python to swap two numbers.
#swapping of two numbers
a=int(input(“Enter the first number: “))
a=int(input(“Enter the first number: “))
print(“Nos. before swap “,a, “and”,b)
a,b=b,a
print(“Nos. after swap”,a,”and”,b)
OUTPUT
Enter the first number: 56
Enter the first number: G
Nos. before swap 56 and 9
Nos. after swap 9 and 56
2. WAP to enter two numbers and print the arithmetic operations
like+,-,*,/,// and%.
p=int(input(“Enter Number-1:”))
q=int(input(“Enter Number-2:”))
r=p+q
print(“Sum=”,r)
r=p-q
print(“Difference=”,r)
r=p*q
print(“Product=”,r)
r=p/q
print(“Division=”,r)
r=p//q
print(“Integer Division=”,r)
r=p%q
print(“Remainder=”,r)
OUTPUT
Enter Number-1: 12
Enter Number-2: 5
Sum=17
Difference= 7
Product= 60
Division= 2.4
Integer Division= 2
Remainder= 2
3. WAP for calculating simple interest.
P=input(“Enter the value for Principal:”)
p=eval(p) #eval is use to convert str to int/float
r=input(“Enter the value for rate:
r=eval(r)
t=input(“Enter the value for
Time:”) t=eval(t)
si=int((p*r*t)/100)
print(“Simple Interest is Rs “,si)
OUTPUT
================RESTART: C:/python programs source
code/si.py==================
Enter the value for Principal: 1000
Enter the value for rate: 2
Enter the value for Time: 2
Simple Interest is Rs 40
>>>
============RESTART: C:/python programs source
code/si.py============
Enter the value for Principal: 2000
Enter the value for rate: 2.5
Enter the value for Time: 3
Simple Interest is Rs 150
4.WAP to check whether entered number is positive or negative
n=int(input(“Enter a
number:”)) if n>=0:
print(n,”is a positive
number”) else:
print(n,”is a negative number”)
Output
Enter a number:-G0
-G0 is a negative number
>>>
Enter a number:45678G
45678G is a positive number
5.WAP to check whether entered character is an uppercase or a lowercase
or a digit or any other character.
c=input(“Enter a character:”)
If c>=’a’ and c<=’z’:
print(c,” is a lowercase
character elif c>=’A’ and
c<=2
print(c, “is an uppercase
character.”) elif c>=’0’ and
c<=’G’:
print(c, “is a
digit”) else:
print(c, “is a special character.”)
Output
Enter a character:t
t is a lowercase character.
>>>
Enter a character:S
S is an uppercase character.
>>>>
Enter a character:6
6 is a digit.
>>>>
Enter a character:$
$ is a special character.
6. WAP to find largest of three numbers.
X=y=z=0
x=int(input(“Enter number 1:”))
y=int(input(“Enter number 2:”))
z=int(input(“Enter number 3:”))
If x>=y and
x>=z:
print(x,”is
largest”)
elif y>=x and y>=z:
print(y, “is largest”)
else:
print(z,”is largest”)
Output
Enter number 1:8G
Enter number 2:8G
Enter number 3:44
8G is largest
>>>
Enter number 1:5
Enter number 2:G8
Enter number 3:123
123 is largest
7. WAP to compute x of given two integers x and y. (xy)
x=int(input(“Enter an integer;”))
y=int(input(“Enter
power:”)) m= 1
for a in range (1,
y + 1) m =m* x
print(“Answer:”,m)
OUTPUT
Enter an integer:5
Enter power:3
Answer: 125
8.WAP to accept a number, find and display whether it’s a Armstrong
number or not.(armstrong number-the sum of cubes of each digits is
equal to the number itself)
n=int(input(“Enter
number:”)) orig=n
sum=0
while
n>0:
rem=n%10
sum=sum+rem*rem*rem n=int(n/10)
if orig==sum: print(orig, is an Armstrong
number.”) else:
print(orig, “is not an Armstrong number.”)
OUTPUT
Enter number:371
371 is an Armstrong number.
Enter number:811
811 is not an Armstrong number.
Enter number:153
153 is an Armstrong number.
9. Write a Program to find factorial of the entered number.
x=int(input(“Enter a number:”))
a=1
f=1
while a<=x:
f=fa
a=a+1
print(“Factorial of”,x,”is:”,f)
OUTPUT
Enter a number:6
Factorial of 6 is: 720
10. WAP to accept an integer number and print its reverse.
n=int(input(“Enter a number:”))
Sum=0
print(“ORIGINAL NUMBER:”,n)
while n>0:
rem=n%10
n=int(n/10)
sum=sum*10+rem
print(“REVERSED NUMBER:”,sum)
OUTPUT
Enter a number:G215
ORIGINAL NUMBER: G215
REVERSED NUMBER: 512G
11. #Write a Program to enter the numbers and to print greatest number
using loop.
big=0
for x in range(1,11); no=int(input(“Enter Number
to Check:
“)) If no>big:
big=no
print (“The greatest Number is: “,big)
OUTPUT
Enter Number to Check: G8
Enter Number to Check: 67
Enter Number to Check: 45
Enter Number to Check: 18G
Enter Number to Check: 43
Enter Number to Check: GG
Enter Number to Check: 100
Enter Number to Check: 34
Enter Number to Check:78
Enter Number to Check: 34
The greatest Number is: 18G
12. Programs to print following patterns
i)
‘”””
Pattern
13
135
1357
13579
for I in range (1, 6) :
x=1
For j in range(1,i+1):
print(x,end=’’)
x=x+2
print()
ii
Patter
n43
21
432
43
for I in range(4):
for j in range(4,1,-1); print(j,end=’’)
Print()
iii
Pattern
ABC
ABC
ABCDE
a=0
x = “ABCDE”
iv)
Pattern
11
111
1111
for I in range(1,5):
for j in range(1,1+1): x = 2
print(x,end=”) print()
v.
pattern
22
444
6666
88888
x=0
for I in
range(1,6): for j
in range(1,1+1):
print(x,end=”)
x=x+2
print()
for I in
x:
a=a+1
for j in range(a):
print(x[i].end
= ‘’)
print()
15. WAP to print GCD and LCM of two numbers
a=int(input(“Enter number1”))
O=int(input(“Enter number2”))
If a>b:
maximum =a
else:
maximum=b
for i in range(maximum,0,-1):
If(a%i==and b%i==0):
print(“GCD:”,i)
break
print(“LCM: (a*b)/i)
OUTPUT
Enter number 1:24
Enter number2:18
GCD: 6
LCM:72
.0
16. WAP to replace every space in a string with a hyphen(“-“)
st=input(“Enter a string”)
sn=len(st)
newst=”
print(“Original string :”,newst)
for i in st:
If i== ‘’:
newst=newst+
else
newst=newst+i
print(“Changed string”, newst)
OUTPUT
Enter a string: WELCOME TO CLASS XI
Original string:
Changed string: WELCOME-TO-CLASS-XI
17. #Write a program to print string in reverse order.
st=input(“Enter a string:”)
print(“Original string is:”,st)
length=len(st)
print(“Reverse:”,end=”)
for I in range(-1,-length-1,-1):
print(st[i],end=”)
OR
st=input(“Enter a string”)
print(“Original string is:”,st)
i=len(st
) rev=”
while i!=0:
rev=rev+st[i-1]
i=i-1
Print(“Reverse:”,rev)
OUTPUT
Enter a string:computer science
Original string is: computer science Reverse, ecneics retupmoc
1G.WAP to traverse a list
i=eval(input(“enter a list:”)) length=len(i)
for a in range(length):
print(“at indexes:”,a,”and”, (a-length), “element:”,i[a])
OUTPUT
enter a list:1G0,88,3,12]
at indexes: 0 and 4 element: G0
at indexes: 1 and -3 element: 88
at indexes: 2 and 2 element: 3
at indexes: 3 and-1 element: 12
enter a list:[G0,78,17.5,8G1,5,8,’hello’]
at indexes: 0 and 6
element: G0 at indexes:
1 and -5 element: 78
at indexes: 2 and -4 element: [7, 5, 8G]
at indexes: 3 and -3 element: 5
at indexes: 4 and-2 element: 8
at indexes: 5 and -1 element: hello
20. WAP to display sum of even and odd values present the list of numbers.
num=eval(input(“Enter
list:”)) so = 0
se = 0
for I in
range(0,len(num)):
if( num[i] \%2==0)
se = se + num[i]
else: so
=s0+num[i]
print(“Sum of even numbers of
list:”,se) print(“Sum of odd
numbers of list:”“so)
OUTPUT
Enter list 1G,1,2,5,6,4.31
Sum of even numbers of list: 12
Sum of odd numbers of list: 18
21. List program – Ask user to enter a list. Then replace all of the entries
in the list that are greater than 10 with 10.
list3=eval(input(“Enter list of numbers:”))
print(“Original list:”, list3)
r-len(list3)
for I in range(r):
if list3[i]>10:
list3[i]=10
print(“Now list is:”,list3)
OUTPUT
Enter list of numbers:[55,4,74,3,6,5,1,62,8]
Original list: [55, 4, 74, 3, -6, 5, 1, 62, 8)
Now list is: [10, 4, 10, 3, -6, 5, 1, 10, 8]