Sei sulla pagina 1di 26

CS: Practical File

Submitted By: Submitted To:


Srijal Kunwar Ms. Preeti Kaushik
2019004324
B.Tech Biotechnology(1st Year)
Table of Contents
Lab 1:
1. WAP to find the sum of digits in python using static
method.
2. WAP to find the sum of digits in python using dynamic
method.
3. WAP to swap two digits in python using dynamic method.
4. WAP to swap two digits in python using static method.
Lab 2:
1. WAP to convert Fahrenheit into Celsius.
2. WAP to calculate bill amount for an item given its quantity,
price, discount and tax.
3. WAP to calculate the total amount of money in a piggy
bank given the coins of Rs.10, Rs.5, Rs.2 and Rs.1.
Lab 3:
1. WAP to check if the entered number is even or odd.
2. WAP to calculate the greatest number among the three.
3. WAP to calculate the greatest number among the three
using nested if.
4. WAP to find the sum of odd numbers and sum of even
numbers in the given range.
Lab 4:
1. WAP to find the sum of digits.
2. WAP to find reverse of a number.
3. WAP to count the number of digits in a number.
4. WAP to find if the given number is Armstrong or not.
Lab 5:
1. WAP to print the Fibonacci series for given number of
turns.
2. WAP that accepts an integer (n) between 1 and 9. Compute
the value of n+nn+nnn.
Lab 6:
1. WAP to find the greatest in a list.
2. WAP to find the sum, product and count the elements in a
list.
3. WAP to find a list and assign +1 to positive, -1 to negative
and 0 for null.
Lab 7:
1. WAP to merge two lists.
2. WAP to count the number of words in a string.
3. WAP to count the uppercase letters and lower case letters
in a string.
4. WAP to create list of even numbers and list of odd numbers
from a list.
Lab 8:
1. WAP to find the factorial using function.
2. WAP to find the greatest among three numbers using
return.
Lab 9:
1. WAP to create a dictionary.
2. WAP using exception handling.
LAB 1:
1. WAP to find the sum of digits in python using static
method.
#developed by: Srijal Kunwar.
a=1 Output:
b=2 3
sum = a + b
print(sum)

2. WAP to find the sum of digits in python using dynamic


method.
#developed by: Srijal Kunwar
a=int(input(“Enter the first number:”)
b= int(input(“Enter the second number:”)
sum=a+b
print(sum)
Output:
Enter the first number: 1
Enter the second number: 3
4
3. WAP to swap two digits in python using dynamic method.
#developed by: Srijal Kunwar
a=int(input(“Enter the first number:”))
b=int(input(“Enter the second number:”))
print (“Before swapping number 1=,”a”, and number 2=”,b )
c=a
a=b
b=c
print(“After swapping number 1=,”a”, and number 2=”,b)
Output:
Enter the first number:2
Enter the second number:1
Before swapping number 1=2 and number 2=1
Before swapping number 1=1 and number 2=2
4. WAP to swap two digits in python using static method
without using third variable.
#developed by: Srijal Kunwar
a=1
b=2
a=a+b
b=a-b
a=a-b
print(“After swapping number 1=,”a”, and number 2=”,b)
Output:
After swapping number 1=2 and number 2=1
Lab 2:
1. WAP to convert Fahrenheit into Celsius.
#developed by: Srijal Kunwar
fahr=float(input("enter temp in fahrenheit="))
cel=(5*fahr-160)/9
print("the temp in celsius=",cel)
Output:
Enter temp in Fahrenheit=42
The temp in Celsius= 5.55

2. WAP to calculate bill amount for an item given its quantity,


price, discount and tax.
#developed by: Srijal Kunwar
CP= float(input("Enter the price of an item="))
n=int(input("Enter the number of items="))
d=float(input("Enter the discount percentage="))
tax=float(input("Enter the tax percentage="))
SP=(CP-d*CP/100)*n
bill=SP+tax*SP/100
print("the bill amount=",bill)
Output:
Enter the price of an item= 500
Enter number of items=2
Enter the discount percentage= 10
Enter the tax percentage=10
The bill amount= 990

3. WAP to calculate the total amount of money in a piggy


bank given the coins of Rs.10, Rs.5, Rs.2 and Rs.1.
#developed by: Srijal Kunwar
a=int(input("no. of rs.10 coins="))
b=int(input("no. of rs.5 coins="))
c=int(input("no. of rs.2 coins="))
d=int(input("no. of rs.1 coins="))
total= 10*a+5*b+2*c+1*d
print("the total amount of money=",total)
Output:
no. of rs.10 coins=1
no. of rs.5 coins=1
no. of rs.2 coins=1
no. of rs.1 coins=1
the total amount of money= 18
Lab 3:
1. WAP to check if the entered number is even or odd.
#developed by: Srijal Kunwar
a=int(input(“Enter a number=”)
if a%2==0:
print(“even”)
else:
print(“odd”)
output:
Enter a number= 28
Even
2. WAP to calculate the greatest number among the three.
#developed by: Srijal Kunwar
a=int(input(“Enter the first number”))
b=int(input(“Enter the second number”))
c=int(input(“Enter the third number”))
if (a>b) and (a>c):
print(“ The largest number is=”,a)
elif (b>a) and (b>c):
print(“The largest number is=”,b)
else:
print(“The largest number is=”,c)
Output:
Enter the first number=1
Enter the second number=2
Enter the third number=3
The largest number is= 3

3. WAP to calculate the greatest number among the three


using nested if.
#developed by: Srijal Kunwar
a=int(input(“Enter the first number”))
b=int(input(“Enter the second number”))
c=int(input(“Enter the third number”))
if (a>b):
if (a>c):
print(“ The largest number is=”,a)
else:
print(“The largest number is=”,c)
else:
if (b>c):
print(“The largest number is=”,b)
else:
print(“The largest number is=”,c)
Output:
Enter the first number=1
Enter the second number=2
Enter the third number=3
The largest number is= 3

4. WAP to find the sum of odd numbers and sum of even


numbers in the given range.
#developed by: Srijal Kunwar
a=int(input(“Enter the first number for range”)
b=int(input(“Enter the second number for range”)
c=b+1
even_sum=0
odd_sum=0
for x in range(a,c)
if x%2==0:
even_sum=x+even_sum
else:
odd_sum=odd_sum+x
print(“The sum of even numbers is=”, even_sum)
print(“The sum of odd numbers is=”, odd_sum)
Output:
Enter the first number for range=1
Enter the second number for range=10
The sum of even numbers is=30
The sum of odd numbers is=25
Lab 4:
1. WAP to find the sum of digits.
#developed by: Srijal Kunwar
a=(int(input(“Enter the number=”)
sum=0
while a>0:
rem=a%10
sum=sum+rem
n=n//10
print(“The sum of digits=”, sum)
Output:
Enter the number=1234
The sum of digits=10
2. WAP to find reverse of a number.
#developed by: Srijal Kunwar
a=(int(input(“Enter the number=”)
rev=0
while a>0:
rem=a%10
rev=rev*10+rem
n=n//10
print(“The reverse of the number=”, rev)
Output:
Enter the number=1234
The reverse of the number=4321

3. WAP to count the number of digits in a number.


#developed by: Srijal Kunwar
a=(int(input(“Enter the number=”)
count=0
while a>0:
count=count+1
n=n//10
print(“The number of digits=”, count)
Output:
Enter the number=1234
The number of digits=4

4. WAP to find if the given number is Armstrong or not.


#developed by: Srijal Kunwar
n=(int(input(“Enter the number=”)
a=n, b=n
count=0
while n>0:
count=count+1
n=n//10
add=0
while b>0:
rem=b%10
add=add+rem**count
if n==add:
print(“Armstrong number”)
else:
print(“Not an Armstrong number”)
Output:
Enter the number=135
Armstrong number
Lab 5:
1. WAP to print the Fibonacci series for given number of
turns.
#developed by: Srijal Kunwar
n=int(input(“Enter the number of terms”)
n1,n2=0,1
count=0
print(“Fibonacci Sequence:”)
while count<n:
print(n1)
nth=n1+n2
n1=n2
n2=nth
count=count+1
Output:
Enter the number of terms=5
011235

2. WAP that accepts an integer (n) between 1 and 9. Compute


the value of n+nn+nnn.
#developed by: Srijal Kunwar
a=int(input(“Enter number between 1 and 9”))
b=0
c=a
d=a
if a<1 and a>9:
print(“error”)
else:
for x in (1,4):
a=(a*10)+c
b=b+a
b=b+c
print(“Value of”,d, “+”, d,d, “+”, d, d, d, “=”, b)
Output:
Enter number between 1 and 9=2
Value of 2+22+222=246
Lab 6:
1. WAP to find the greatest in a list.
#developed by: Srijal Kunwar
list=[]
n=int(input(“Enter the number of elements”))
print(“Enter the list=”)
for x in range (0,n):
elements=input()
list.append(elements)
num=list[0]
for i in range (1,n):
if(num<list[i]):
num=list[i]
print(“The maximum number is=”,num)
Output:
Enter the number of elements=5
Enter the list=
[0,1,3,33,232]
The maximum number is=232
2. WAP to find the sum, product and count the elements in a
list.
#developed by: Srijal Kunwar
list=[]
n=int(input(“Enter the number of elements”))
print(“Enter the list=”)
for x in range (0,n):
elements=input()
list.append(elements)
sum=0
product=1
for x in range (0,n):
sum=sum+list[x]
product=product*list[x]
print(“The sum of list elements is=”,sum)
print(“The product of list elements is =”,product)
Output:
Enter the number of elements=3
Enter the list=
[1,2,3]
The sum of list elements is=6
The product of list elements is=6

3. WAP to find a list and assign +1 to positive, -1 to negative


and 0 for null.
#developed by: Srijal Kunwar
list=[]
n=int(input(“Enter the number of elements”))
print(“Enter the list=”)
for x in range (0,n):
elements=int(input())
list.append(elements)
for x in range(0,n):
if list[x]>0:
list[x]=1
elif list[x]<0:
list[x]=-1
elif list[x]=0:
list[x]=0
print(list)
Output:
Enter the number of elements=5
Enter the list=
[22,89,56,-37,0]
[1,1,1,-1,0]
Lab 7:
1. WAP to merge two lists.
list1=[]
list2=[]
n1=int(input(“Enter the number of elements in first list=”))
n2=int(input(“Enter the number of elements in second list=”))
for x in range (0,n1):
elements=int(input())
list.append(elements)
for x in range (0,n2):
elements=int(input())
list2.append(elements)
X=len(list2)
for y in range(0,X):
list1.append(list2[y])
list1.sort()
print(list)
Output:
Enter the number of elements for first list=3
Enter the number of elements for second list=2
[1,12,34,45,60]
2. WAP to count the number of words in a string.
#developed by: Srijal Kunwar
a= str(input(“Enter a string:”))
count_space=0
count_words=0
for i in a:
if i== ‘ ’:
count_space=+1
count_words=count_space+1
print(“The number of words in the string is=”, count_words)
Output:
Enter a string: A program
The number of words in the string is= 2

3. WAP to count the uppercase letters and lower case letters


in a string.
s=str(input(“Enter a string:”)
x=len(s)
count_upper=0
count_lower=0
for i in s:
if i> “A” and i< “Z”:
count_upper=+1
elif i> “a” and i< “z”:
count_lower+=1
print(“The number of upper cases letters are=”, count_upper)
print(“The number of lower cases letters are=”, count_lower)
Output:
Enter a string: PoTato
The number of upper cases letters are= 2
The number of lower cases letters are= 3
4. WAP to create list of even numbers and list of odd numbers
from a list.
#developed by: Srijal Kunwar
l=[]
n=int(input(“Enter the number of elements”))
for x in range (0,n):
elements=int(input())
list.append(elements)
print(“list is=”,l)
even=[]
odd=[]
for i in l:
if i%2==0:
even.append(i)
else:
odd.append(i)
print(“Even list is=”, even)
print(“Odd list is=”,odd)
Output:
Enter the number of elements=5
List is=
[10,83,47,20,21]
Even list is=
[10,20]
Odd list is=
[83,47,21]
Lab 8:
1. WAP to find the factorial using function.
#developed by: Srijal Kunwar
def fact (n):
f=1
for x in range(0,n)
f=f*x
print(“factorial is=”, f)
num=int(input(“Enter the number=”))
fact(num)
Output:
Enter the number =3
Factorial is=6
2. WAP to find the greatest among three numbers using
return.
#developed by: Srijal Kunwar
def greatest(a, b,c)
if (a>b) and (a>c):
return a
elif (b>a) and (b>c):
return b
else:
return c
n1=int(input(“Enter the first number=”))
n2=int(input(“Enter the second number=”))
n3=int(input(“Enter the third number=”))
grt=greatest(n1,n2,n3)
print(“Greatest=”,grt)
Output:
Enter the first number=1
Enter the second number=2
Enter the third number=3
Greatest = 3

Lab 9:
1. WAP to create a dictionary.
#developed by: Srijal Kunwar
keys=[‘name’, ‘age’, ‘place’]
values=[‘Srijal’, 19, ‘Kathmandu’]
mydict={k:v for k, v in zip(keys, values)}
print(“Dictionary items:”,mydict)
Output:
Dictionary items:
name: Srijal
age :19
place: Kathmandu

2. WAP using exception handling.


#developed by: Srijal Kunwar
a=int(input(“Enter the first number”))
b=int(input(“Enter the second number”))
try:
result=a/b
print(“div=”, result)
except:
print(“a number cannot be divided by zero”)
else:
print(“program ended without any error”)
Output:
Enter the first number=10
Enter the second number=2
div=5

Potrebbero piacerti anche