BMS COLLEGE OFENGINEERING
(Autonomous college under VTU)
Bull Temple Rd, Basavanagudi, Bengaluru, Karnataka 560019
2020
Department of Computer Applications
Report is submitted for the fulfillment of the AAT Work in the subject
“Python Programming”
(22MCA1PCPY)
I Semester
MCA
Name of Student: Saransh Vatsa Register Number:
Date of Submission: 04/03/2023
Under the Guidance
RV RAGHAVENDRA RAO
(Assistant Professor)
22MCA1PCPY: Python Programming
Table of Contents
1. Python program to find the geometric mean of n numbers 2
2. Python program to find the sum of the digits of an integer using a while loop 3
3. Python program to display all the multiples of 3 within the range 10 to 50 4
4. Python program to display all integers within the range 100-200 whose sum of digits is
an even number 5
5. Python program to check whether the given integer is a prime number or not 6
6. Python program to generate the prime numbers from 1 to N 7
7. Python program to find the roots of a quadratic equation 8
8. Python program to print the numbers from a given number n till 0 using recursion 9
9. Python program to find the factorial of a number using recursion 10
10. Python program to display the sum of n numbers using a list 11
Department of Computer Applications, BMSCE Page 1
22MCA1PCPY: Python Programming
Q1. Python program to find the geometric mean of n numbers.
Ans:
n=int(input("Enter n "))
prod=1
for i in range(n):
a=int(input("Enter the element "))
prod=prod*a
gm=prod**(1/n)
print("GM is ",gm)
Output:
Department of Computer Applications, BMSCE Page 2
22MCA1PCPY: Python Programming
Q2. Python program to find the sum of the digits of an integer using a while loop.
Ans:
a=int(input("Enter a number "))
str_a=str(a)
length=len(str_a)
sum=0
for i in range(1,length+1):
sum=sum+(a%10)
a=a//10
print(sum)
Output:
Department of Computer Applications, BMSCE Page 3
22MCA1PCPY: Python Programming
Q3. Python program to display all the multiples of 3 within the range 10 to 50.
Ans:
for i in range(10,51):
if i%3==0:
print(i)
Output:
Department of Computer Applications, BMSCE Page 4
22MCA1PCPY: Python Programming
Q4. Python program to display all integers within the range 100-200 whose sum of digits is
an even number .
Ans:
for i in range(100,201):
a=i
sum=0
str_a=str(a)
length=len(str_a)
for x in range(1,length+1):
sum=sum+(a%10)
a=a//10
if sum%2==0:
print(i)
Output:
Department of Computer Applications, BMSCE Page 5
22MCA1PCPY: Python Programming
Q5. Python program to check whether the given integer is a prime number or not.
Ans:
n=int(input("Enter a number "))
count=0
r=int(n//2)
for i in range(1,r+2):
if n==0:
print("It is not a prime")
break
if n==1:
print("It is not a prime")
break
if n==2:
print("It is a prime")
break
if n%i==0:
count=count+1
if count>1:
print("It is not a prime number")
break
if count<2 and n!=1 and n!=2 and n!=0:
print("It is a prime")
Output:
Department of Computer Applications, BMSCE Page 6
22MCA1PCPY: Python Programming
Q6. Python program to generate the prime numbers from 1 to N.
Ans:
n=int(input("Enter n "))
for j in range(1,n+1):
count=0
r=int(j//2)
for i in range(1,r+2):
if j==1:
break
if j==2:
print(2)
break
if j%i==0:
count=count+1
if count>1:
break
if count<2 and j!=1 and j!=2:
print(j)
Output:
Department of Computer Applications, BMSCE Page 7
22MCA1PCPY: Python Programming
Q7. Python program to find the roots of a quadratic equation.
Ans:
a=int(input("Enter the coefficient of x^2 "))
b=int(input("Enter the coefficient of x "))
c=int(input("Enter c "))
d=b**2-4*a*c
root1=(-b+d**(1/2))/2*a
root2=(-b-d**(1/2))/2*a
print(root1," ",root2)
Output:
Department of Computer Applications, BMSCE Page 8
22MCA1PCPY: Python Programming
Q8. Python program to print the numbers from a given number n till 0 using recursion.
Ans:
def printlist(n):
if(n==0):
print(0)
return 0
print(n)
printlist(n-1)
n=int(input("Enter the number "))
printlist(n)
Output:
Department of Computer Applications, BMSCE Page 9
22MCA1PCPY: Python Programming
Q9. Python program to find the factorial of a number using recursion.
Ans:
def factorial(n):
if n==2:
return 2
x=n*factorial(n-1)
return x
n=int(input("Enter the number "))
a=factorial(n)
print(a)
Output:
Department of Computer Applications, BMSCE Page 10
22MCA1PCPY: Python Programming
Q10. Python program to display the sum of n numbers using a list
Ans:
list=[1]
n=int (input("Enter the number of elements "))
for i in range(n):
a=int (input ("Enter element "))
list.append(a)
sum=0
for i in range(n) :
sum=sum+list.pop ()
print("sum = ",sum)
Output:
Department of Computer Applications, BMSCE Page 11