100% found this document useful (1 vote)
341 views

Unit 2 & Unit 3 - Python - Practice - Problems

Python_Basics_Practice_Problems

Uploaded by

Rohini Aravindan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
341 views

Unit 2 & Unit 3 - Python - Practice - Problems

Python_Basics_Practice_Problems

Uploaded by

Rohini Aravindan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Easwari Engineering College

(Autonomous)
Department of Artificial Intelligence & Data Science
Year/Dept: I/AI&DS & I/AI&ML

Unit 2 & Unit 3 Practice Programs :


Q1 Execute a python program to read two integer and two floating point values and
print the same
Source code
a=int(input("Enter value of a:"))
b=int(input("Enter value of b:"))
c=float(input("Enter value of c:"))
d=float(input("Enter value of d:"))

print("Value of two integers :",a,b)


print("Value of two floating point :",c,d)

OUTPUT

Q2 Execute a python program to find the sum of three integer numbers

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

Q3 Write a python program to execute for swapping two numbers


Source code
a=int(input("Enter value of a:"))
b=int(input("Enter value of b:"))
c=a
a=b
b=c
print("New value of a:",a)
print("New value of b:",b)
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

Q5 Execute a python program to find out greatest of two numbers


Source code

a=int(input("Enter value of a:"))


b=int(input("Enter value of b:"))
if(a>b):
print(a,"is greatest")
else:
print(b,"is greatest")
OUTPUT
Q6 Execute a python program to find out a number is positive or negative
Source code
num=int(input("Enter a number:"))
if (num>0):
print(num,"is positive")
else:
print(num,"is negative")

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

Q9 Write and Execute a python program to evaluate the given expression:


Source code

a^2 + 2ab + b^2


a=int(input("Enter value of a:"))
b=int(input("Enter value of b:"))
print("a^2 + 2ab + b^2=",a*a + 2*a*b + b*b)
OUTPUT
Q10 Write and Execute a python program to find out area of a circle
Source code
pi = 3.14
r=float(input("Enter value of radius:"))
Area=pi * r * r
print("Area =",Area)

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

n = int(input("Enter value of n:"))


i=1
s=0
while(i<=n):
s=s+i
i=i+1
print("Sum=",s)
Output
Q21 Write a python program to find factorial of 5
Source code

i=1
f=1
while(i<=5):
f=f*i
i=i+1
print("Factorial of 5:",f)
Output

Q22 Write a python program to find factorial of given number


Source code

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

n=int(input("Enter number of iterations:"))


i=1
while(i<=n):
a=int(input("Enter value of a:"))
b=int(input("Enter value of b:"))
if(a>b):
print(a,"is greater")
else:
print(b,"is greater")
i=i+1
Output
Q24 Write and execute a python program to find LCM of 2 numbers

Source code

a=int(input("Enter value of a:"))


b=int(input("Enter value of b:"))
if(a>b):
x=a
else:
x=b
while(1):
if(x%a==0 and x%b==0):
print("LCM =",x)
break
x=x+1
Output

Q25)Write and execute a python to print reverse of a number


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

Q26) Write and execute a python to check whether palindrome or not


Source code

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 the number of terms:"))


t1=0
t2=1
i=2
print(t1)
print(t2)
while(i<n):
nxt=t1+t2
t1=t2
t2=nxt
i=i+1
print(nxt)
Output
Q28) Write and execute a python to check a number is Armstrong number or not
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

n=int(input("Enter the limit:"))


i=1
s=0
while(i<=n):
s=s+(i*i)
print(s)
i=i+1
print("Sum:",s)
Output

Q30) write and execute a python program to print


1
12
123
1234
Source code
i=1
r=int(input("enter rows:"))
while(i<=r):
j=1
while(j<=i):
print(j,end="")
j=j+1
print()
i=i+1

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

Q31) write and execute a python program to print


*
**
***
****
*****

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

Q36) Write a python program to execute the following using recursions:


a) factorial of a number
Source code
def fact(n):
f=1
if n==1:
return 1
else:
f=n*fact(n-1)
return f
n=int(input("Enter a number : "))
print(fact(n))
Output
b) Armstrong number using functions:
Source code
def armstrong(n):
if(n==0):
return n
else:
rem = n%10
n=n //10
s=armstrong(n)+(rem*rem*rem)
return s
n=int(input("Enter a number :"))
o=n
s2=armstrong(n)
if (s2==o):
print(o,"is armstrong number")
else:
print(o,"is not an armstrong number")
Output
c) Fibonacci series using recursions:
Source code
def fibonacci(n):
if(n <= 1):
return n
else:
s = (fibonacci(n-1) + fibonacci(n-2))
return s
n = int(input("Enter number of terms:"))
for i in range(n):
print(fibonacci(i))
Output

d) Palindrome using function:


Source code
def palindrome(n,s):
if(n==0):
return s
else:
rem=n%10
n=n//10
s=(s*10) + rem
return palindrome(n,s)
n=int(input("Enter a number :"))
o=n
s=palindrome(n,0)
if (s==o):
print(o,"is palindrome")
else:
print(o,"is not a palindrome")
Output

e) Reverse of a number using function:


Source code
def rev(n,s):
if(n==0):
return s
else:
rem=n%10
n=n//10
s=(s*10) + rem
return rev(n,s)

n=int(input("Enter a number :"))


o=n
s=rev(n,0)
print("Reverse :",s)
Output

Prepared by,
Rohini A,
AP / AI&DS

You might also like