Unit 2 & Unit 3 - Python - Practice - Problems
Unit 2 & Unit 3 - Python - Practice - Problems
(Autonomous)
Department of Artificial Intelligence & Data Science
Year/Dept: I/AI&DS & I/AI&ML
OUTPUT
Source code
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
c=int(input("Enter third number:"))
sum = a + b + c
print("sum of three integer numbers =",sum)
OUTPUT
Q4 Execute a python program to read two integer numbers and perform the following
arithmetic operations
I. Subtraction
II. Addition
III. Multiplication
IV. Division
V. Modulus
Sourcecode
a=int(input("Enter the value of a: "))
b=int(input("Enter the value of b:"))
c=a-b
d=a+b
e=a*b
f=a/b
g=a%b
print("Subtraction:",c)
print("Addition:",d)
print("Multiplication:",e)
print("Division:",f)
print("Modulus:",g)
OUTPUT
OUTPUT
Q7 Execute a python program to find out whether a year is leap year or not
Source code
yr=int(input("Enter a year:"))
if(yr%4==0):
print(yr,"is a leap year")
else:
print(yr,"is not a leap year")
OUTPUT
Q8 Execute a python program to find out area of triangle
Source code
b=int(input("Enter base:"))
h=int(input("Enter height:"))
area = 1/2 * b * h
print("Area of triangle =",area)
OUTPUT
OUTPUT
Q11 Write and execute a python program to display first 5 natural numbers
SOURCE CODE
i=1
while(i<=5):
print(i)
i=i+1
Output
Q12 Write and execute a python program to display first 10 natural numbers
SOURCE CODE
i=1
while(i<=10):
print(i)
i=i+1
Output
Q13 Write and execute a python program to print first 100 whole numbers
SOURCE CODE
i=0
while(i<=100):
print(i)
i=i+1
Output
Q14) Write and execute a python program to find out the sum of first 10 natural
numbers.
Source code
i=1
s=0
while(i<=10):
s=s+i
i=i+1
print("Sum of first 10 natural numbers :",s)
Output
Q15) Write and execute a python program to print first 10 even numbers
Source code
i=2
while(i<=10):
print(i)
i=i+2
Output
Q16) Write and execute a python program to print odd numbers within 0 to 10
Source code
i=1
while(i<=10):
print(i)
i=i+2
Output
Q17 Write and execute a python program to find out the sum of odd numbers till 10.
Source code
i=1
x=0
while(i<=10):
x=x+i
i=i+2
print("sum of odd numbers till 10:",x)
Output
Q18 Write and execute a python program to find out the sum of even numbers till 10.
Source code
i=2
x=0
while(i<10):
x=x+i
i=i+2
print("sum of even numbers till 10:",x)
Output
Q19 Write and execute a python program to print first n natural numbers
Source code
i=1
n=int(input("Enter value of n:"))
print("first n natural numbers :")
while(i<=n):
print(i)
i=i+1
Output
Q20 Write and execute a python program to print sum of first n natural numbers
Source code
i=1
f=1
while(i<=5):
f=f*i
i=i+1
print("Factorial of 5:",f)
Output
i=1
f=1
n=int(input("Enter the number:"))
while(i<=n):
f=f*i
i=i+1
print("Factorial of",n,":",f)
Output
Q23 Write a python program to find greatest of 2 numbers asking n times the input
Source code
Source code
n=int(input("Enter a number:"))
rev=0
while(n>0):
rem=n%10
rev=rev*10 + rem
n=n//10
print("Reverse:",rev)
Output
n=int(input("Enter a number:"))
orig=n
rev=0
while(n>0):
rem = n%10
rev=rev * 10 + rem
n=n//10
print("Reverse:",rev)
if (rev==orig):
print("Number is palindrome")
else:
print("Number is not palindrome")
Output
Q27) Write and execute a python to print fibonacci series
Source code
n=int(input("Enter a number:"))
orig=n
s=0
while(n>0):
rem=n%10
s=s+(rem*rem*rem)
n=n//10
if(s==orig):
print("It is an Armstrong number")
else:
print("Not an Armstrong number")
Output
Q29) Write and execute a python to print sum of squares of natural number
Source code
OR
n=int(input("enter n:"))
i=1
j=1
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end="")
print()
Output
Source code
n=int(input("enter n:"))
i=1
j=1
for i in range(1,n+1):
for j in range(1,i+1):
print("* ",end="")
print()
Output
Q32) write and execute a python program to display natural numbers upto 100 that are
divisible by 9 and 11 but not by 6
Source code
i=1
while(i<=100):
if(i%9==0 and i%11==0 and i%6!=0):
print(i)
i=i+1
Output
Q33) write and execute a python program to display prime numbers between 1 to 100
Source code
n=int(input("Enter a number:"))
for i in range(2,n+1):
c=0
for j in range(2,i//2+1):
if(i%j==0):
c=c+1
break
if(c==0):
print(i)
Output
Q34) Write and execute a python program to print sum of cubes of n natural numbers
Source code
n=int(input("Enter a number:"))
i=1
s=0
while(i<=n):
s=s+(i*i*i)
print(s)
i=i+1
print("Sum of cubes of first",n,"natural numbers:",s)
Output
Q35) Write and execute a python program to print sum of digits in a number
Source code
n=int(input("Enter a number:"))
s=0
while(n>0):
rem=n%10
s=s+rem
n=n//10
print("Sum of digits=",s)
Output
Prepared by,
Rohini A,
AP / AI&DS