Programs
1. WAP to do addition , subtraction , multiplication and division
Solution :
A = int (input (“enter first number =”)
B = int (input (‘enter second number =”)
C = a+b
D=a-b
E=a=b
F=a\b
Print (C,D,E,F)
2. WAP to find square and cube of a number
Solution :
a = int(input(“enter a number= “)
Square = a*a
Cube = A*a*a
Print (“the square of the number is =”,Square)
Print (“the cube of the number is =”,Cube )
3. Write a program to compute the area and circumference of circle
Solution :
r=int(input(“input radius”))
pi=(22/7)
c=2*pi*r
area=pi*r**2
print('circumference =',c)
print('area =',area)
4. Write a program to compute area and perimeter of square
Solution :
s=int(input('input side'))
perimeter=4*s
area=s**2
print('perimeter =',perimeter)
print('area =',area)
5. Write a program to compute area and perimeter of rectangle
Solution :
l=int(input('input length'))
b=int(input('input breadth'))
perimeter=2*(l+b)
area=l*b
print('perimeter =',perimeter)
print('area =',area)
6. WAP to swap two numbers
Solution :
a=int (input (“enter a number= “))
b = int (input (“enter a number= “))
(a,b)=(b,a)
Print (“the first numbers =”,a)
Print (“the second numbers =,b)
7. Write a Python program to check if the number entered is odd or even
Solution :
number = int(input("Enter a number: "))
if number%2 == 0:
print("Given number is Even")
else:
print("Given number is Odd")
8. Python program to check if the number is divisible by both 5 and 7 or not
Solution :
number = int(input("Enter a number: "))
if (number%5 == 0) and (number%7 == 0):
print("Number is divisible by both 5 and 7")
else:
print("Number is not divisible by both 5 and 7")
9. A store charges RS 120 per item if you buy less than 10 items. If you buy
between 10 and 99 items, the cost is RS 100 per item. if you buy more items,
the cost is RS 70 the user how many items they are buying and print the total
cost.
Solution :
item = int(input("Enter the total number of items = "))
if item < 10 :
print("Total cost = ",item * 120 , "rs")
elif item>=10 and item <= 99 :
print("Total cost = ",item * 100, "rs")
else :
print("Total cost = ",item * 70 , "rs")
10. Write a program to input length of three side of a triangle, then check if these
side will form a triangle or not.
Solution :
a = int (input ("Enter the first side of triangle = "))
b = int (input ("Enter the second side of triangle = "))
c = int (input ("Enter the third side of triangle = "))
if a < b + c and b < a + c and c < a + b :
print ("It forms a triangle")
else :
print ("It cannot form a triangle")
11. Write a short program to input a digit and print it in words. Paste your code
below.(Hint Make use of if statements)
Solution :
a = int (input ("enter the digit (1-10) = "))
if a ==1 :
print ("one ")
elif a ==2 :
print ("two")
elif a ==3 :
print ("three")
elif a == 4:
print ("four ")
elif a == 5:
print ("five")
elif a == 6:
print ("six ")
elif a == 7:
print ("seven")
elif a == 8:
print ("eight ")
elif a == 9:
print ("nine")
elif a == 10 :
print ("ten")
else:
print ("digit is out of range ")
12. Write a program to input 3 sides of triangle and print whether it is an
equilateral, scalene or isosceles triangle.
Solution :
side1 = int(input("Enter 1st side of triangle = "))
side2 = int(input("Enter 2nd side of triangle = "))
side3 = int(input("Enter 3rd side of triangle = "))
if side1 == side2 == side3:
print("It is a equilateral triangle ")
elif side1 == side2 != side3 or side1 != side2 == side3 or side1 == side3 !=
side2 :
print("It is a isosceles triangle ")
elif side1 != side2 != side3 :
print("It is a scalene triangle ")
13. Write a program to take an integer "a" as an input and check whether it ends
with 4 or 8. if its ends with 4, print "ends with 4", if it ends with 8, print "ends
with 8", otherwise print "ends with neither".
Solution :
a = int(input("Enter a number = "))
if a % 10 == 4 :
print(a,"end with 4")
elif a% 10 == 8 :
print(a,"end with 8 ")
else :
print(a,"end with neither ")
14. Write a program to take N(N>20) as an input from the user. print number from
11 to N. When the number is multiple of 3, print "tipsy". when it is multiple of
7 print "topsy". When it is a multiple of both print "tipsyTopsy".
Solution :
By for loop method
n = int(input("enter a number greater than 20 = "))
for i in range(11,n+1):
if i % 3 == 0 and i % 7==0 :
print(i,"tipsytopsy")
elif i % 3==0 :
print(i,"tipsy")
elif i % 7==0 :
print(i,"topsy")
By while loop method
n = int(input("enter a number greater than 20 = "))
i = 11
while i <= n :
if i % 3 == 0 and i % 7==0 :
print(i,"tipsytopsy")
elif i % 3==0 :
print(i,"tipsy")
elif i % 7==0 :
print(i,"topsy")
i += 1
15. Write a complete python program to do the following:
(a) read an integer X.
(b) determine the number of digit n in X.
(c) from an integer Y that has the number of digit n at tens place and the
most significant Digit of X at one’s place.
(d) output Y
Solution :
x = input("enter a number = ")
n = len(x)
y = str(n) + x[0]
print("new number = ",y )
16. Write programs using nested loops to produce the following pattern:
A
A B
A B C
A B CD
A B CDE
A B CDEF
Solution :
for i in range(6):
for j in range(6):
if i >= j :
print(chr(65+j)+" ",end="")
print()
17. Given three number A, B and C. Write a program to write their values in an
ascending order.
For example A = 12, B = 10 and C = 15, your program should print out:
Solution :
Smallest number = 10
Next higher number = 12
Highest number = 15
a = int(input("Enter 1st number = "))
b = int(input("Enter 2nd number = "))
c = int(input("Enter 3rd number = "))
if a > b and b > c :
max, mid, low = a, b, c
elif b > a and a > c :
max, mid, low = b, a, c
elif c > b and b > a :
max, mid, low = c, b, a
elif c > b and c > a and a > b :
max, mid, low = c, a, b
elif b > c and b > a and a < c :
max, mid, low = b, c, a
elif a > b and a > c and b < c :
x, mid, low = a, c, b
print("Smallest number :- ",low)
print("Next higher number :- ",mid)
print("Highest number :- ",max)
18. Write a python script to input temperature. then ask them what unit, Celsius
or Fahrenheit, the temperature is in. Your program should convert the
temperature to other unit
The conversions are
F = 9/5 C + 32
C = 5/9(F – 32).
Solution :
temp = float(input("Enter temperature = "))
unit = input("Enter unit of temperature (c or f) =")
if unit=="F" or unit == "f" :
c = (5/9)*(temp - 32)
print("Temperature in celsius = ",c,"C")
elif unit=="C" or unit == "c" :
f = (9/5)*temp + 32
print("Temperature in Fahrenheit = ",f,"F")
else :
print("Invalid unit ")
19. Write a program to print absolute value of a number. Absolute value of a
number is:
Solution :
num = int(input("Enter a number :-"))
if num >= 0 :
print(num)
else :
print(num*-1)
20. Write a program to input marks in 3 subjects; compute average and then
calculate grade as per following guidelines:
Grade Remarks Marks
A (Level 4, above agency-normalized standards) 80% and
above
B (Level 3, at agency-normalized standards) 70-79%
C (Level 2, below, but approaching agency-normalized 60-69%
standards)
D (Level 1, well below agency-normalized standards) 50-59%
E (Level 1-, too below agency-normalized standards) 40-49%
R (Remedial standards) 39% and
below
Solution :
mark1 = int (input ("Enter the mark for subject 1 :- "))
mark2 = int (input ("Enter the mark for subject 2 :- "))
mark3 = int (input ("Enter the mark for subject 3 :- "))
print ("Average marks is :- ", (mark1 + mark2 + mark3) / 3 )
per = ((mark1 + mark2 + mark3) / 300) * 100
print("Percentage :-",per)
if per >= 80 :
print ("Grade is 'A'")
elif per >= 70 and per <= 79 :
print ("Grade is 'B'")
elif per >= 60 and per <= 69 :
print ("Grade is 'C'")
elif per >= 50 and per <= 59 :
print ("Grade is 'D'")
elif per >= 40 and per <= 49 :
print ("Grade is 'E'")
elif per <= 39 :
print ("Grade is 'R'")
21. Write a program to check whether the entered year is leap year or not.
Solution :
year = int(input("Enter any year "))
if year % 100 == 0:
if year % 400 == 0:
print("Year is LeapYear ")
else:
print("Year is not LeapYear")
elif year %4 ==0:
print("Year is LeapYear ")
else:
print("Year is not LeapYear")
22. Write a program to check whether any word is a part of sentence or not
Solution :
line = input("Enter any statement")
word = input("Enter any word")
if word in line:
print("Yes ", word, "is a part of ",line)
else:
print("No", word ," is not part of ",line)
23. Write a program to print the multiplication table of any number entered.
Solution :
num = int(input("Enter any number "))
for i in range(1,11):
print(num,'x',i,'=',num*i)
24. Write a program to find the sum of all number divisible by 7 between 1 to 100
Solution :
sum=0
for i in range(1,101):
if i % 7 == 0:
sum+=i
print("total of number divisible by 7 between 1 to 100 is ",sum)
25. WAP to enter any number and find its reverse
Solution :
num = int(input("Enter any number "))
rev = 0
num2=num
while num>0:
rem = num % 10
rev = rev * 10 + rem
num //=10
print("Reverse of ",num2," is ",rev)
26. Write a program to input a number and test if it is a prime number or not
Solution :
num = int(input("Enter any number :"))
lim = num//2 + 1
for i in range(2,lim):
rem = num % i
if rem == 0:
print(num," is not a prime number ")
break
else:
print(num," is a prime number ")
**********