0% found this document useful (0 votes)
24 views

Python Exercise-5

Uploaded by

laughoutloudzero
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Python Exercise-5

Uploaded by

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

Rahul Suresh 22IZ029

Problem Solving & Python Programming Lab


Exercise-5

1. To find Average of n Numbers

Code:
sum=0
n=int(input("Enter how many numbers you want to add: "))
for i in range(n):
num=int(input("Enter a number: "))
sum=sum+num
average=sum/n
print("Average: ",average)

Output:
Enter how many numbers you want to add: 5
Enter a number: 5
Enter a number: 5
Enter a number: 5
Enter a number: 5
Enter a number: 5
Average: 5.0
Rahul Suresh 22IZ029

2. Checking input number for Odd or Even

Code:
num=int(input("Enter a number: "))
if (num%2==0):
print(num,"is an even number")
else:
print(num,"is an odd number")

Output:
Enter a number: 3
3 is an odd number
Rahul Suresh 22IZ029

3. Print Factors of a Number

Code:
l=[]
num=int(input("Enter the number: "))
for i in range(1,num+1):
if (num%i==0):
l.append(i)
print("Factors of",num,"are: ")
for j in range(len(l)):
print (l[j])

Output:
Enter the number: 6
Factors of 6 are:
1
2
3
6
Rahul Suresh 22IZ029

4. Find Largest among n Numbers

Code:
l=[]
n=int(input("How many numbers do you want to add: "))
for i in range(n):
num=int(input("Enter a number: "))
l.append(num)
print("Largest number is",max(l))

Output:
How many numbers do you want to add: 5
Enter a number: 56
Enter a number: 278
Enter a number: 2564
Enter a number: 43
Enter a number: 567
Largest number is 2564
Rahul Suresh 22IZ029

5. Print Multiplication Table of input Number

Code:
n=int(input("Enter the number you want the multiplication table: "))
print("The Multiplication for",n,"is: ")
for i in range(1,11):
print (n,"*",i,"=",n*i)

Output:
Enter the number you want the multiplication table: 5
The Multiplication for 5 is:
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Rahul Suresh 22IZ029

6. Fibonacci Series

Code:
def f(n):
if n<0:
print("invalid")
elif n==0:
return 0
elif n==1 or n==2:
return 1
else:
return f(n-1) + f(n-2)
x=int(input("Enter a number: "))
a=f(x)
print("Fibonacci: ",a)

Output:
Enter a number: 9
Fibonacci: 34
Rahul Suresh 22IZ029

7. Checking for Vowel

Code:
l=[]
n=input("Enter a string: ")
a=["a","A","e","E","i","I","o","O","u","U"]
for i in n:
l.append(i)
for j in l:
if j in a:
print (j)

Output:
Enter a string: Apples
A
e
Rahul Suresh 22IZ029

8. Swapping Two Numbers

Code:
a=int(input("Enter a number: "))
b=int(input("Enter another number: "))
c=a
a=b
b=c
print ("The first number is swapped to",a)
print ("The second number is swapped to",b)

Output:
Enter a number: 5
Enter another number: 6
The first number is swapped to 6
The second number is swapped to 5
Rahul Suresh 22IZ029

9. Second largest among three numbers


Code:
a=int(input("Enter 1st number: "))
b=int(input("Enter 2nd number: "))
c=int(input("Enter 3rd number: "))
if (a>b):
if (c>a):
print (a,"is the second largest number")
elif (a>c):
print (b,"is the second largest number")
elif (b>a):
if(c>b):
print (b,"is the second largest number")
elif (b>c):
print (c,"is the second largest number")
elif (c>a):
if(b>c):
print (c,"is the second largest number")
elif (c>b):
print (a,"is the second largest number")

Output:
Enter 1st number: 5
Enter 2nd number: 3
Enter 3rd number: 15
5 is the second largest number
Rahul Suresh 22IZ029

10. Finding simple interest and compound interest


Code:
p=float(input("Enter the principal amount: "))
t=float(input("Enter number of years: "))
r=float(input("Enter rate of interest: "))
n=float(input("Enter the number of times interest is compounded
per year: "))
s=(p*t*(r/100))
print("Simple interest =",s)
a=p+s
m=1+((r/100)/n)
c=(p*(m**(n*t)))-p
print("Compound Interest = ",c)
print("Amount after simple interest = Rs.",a)
b=p+c
print("Amount after compound interest = Rs.",b)

Output:
Enter the principal amount: 1000
Enter number of years: 5
Enter rate of interest: 5
Enter the number of times interest is compounded per year: 2
Simple interest = 250.0
Compound Interest = 280.08454419635655
Amount after simple interest = Rs. 1250.0
Amount after compound interest = Rs. 1280.0845441963565
Rahul Suresh 22IZ029

11. Greatest Common Divisor(GCD)

Code:
import math
a=int(input("Enter a number: "))
b=int(input("Enter another number: "))
print("The gcd of",a,"and",b," : ", end="")
print(math.gcd(a, b))

Output:
Enter a number: 60
Enter another number: 48
The gcd of 60 and 48 : 12
Rahul Suresh 22IZ029

12. Celsius to Fahrenheit

Code:
temp=int(input("Enter temperature in Celsius: "))
k=temp+273
print("Temperature in Kelvin =",k)

Output:
Enter temperature in Celsius: 30
Temperature in Kelvin = 303
Rahul Suresh 22IZ029

13. Roots of Quadratic Roots

Code:
import math
eq=input("Enter the equation correctly in the order (ax^2+bx+c=0): ")
a=int(input("Enter the coefficient of x^2 :"))
b=int(input("Enter the coefficient of x :"))
c=int(input("Enter the constant value: "))
w=((b**2)-(4*a*c))
v=math.sqrt(w)
x=(-b+v)/(2*a)
y=(-b-v)/(2*a)
print("The roots of the equation",eq,"are",x,"and",y)

Output:
Enter the equation correctly in the order (ax^2+bx+c=0): a
Enter the coefficient of x^2 :2
Enter the coefficient of x :-1
Enter the constant value: -6
The roots of the equation a are 2.0 and -1.5
Rahul Suresh 22IZ029

14. Calculate nPr and nCr


Code:
def fact(k):
Output:
f=i=1
Enter the Value of n: 5
while i<=k:
Enter the Value of r: 2
f = i*f
i += 1
Permutation (nPr) = 20.0
return f
def findperm(x, y): Combination (nCr) = 10.0

num = fact(x)
den = fact(x - y)
perm = num / den
return perm
def findcomb(x, y):
num = fact(x)
den = fact(x - y)
den = fact(y) * den
comb = num / den
return comb
print("Enter the Value of n: ", end="")
n = int(input())
print("Enter the Value of r: ", end="")
r = int(input())
print("\nPermutation (nPr) =", findperm(n, r))
print("Combination (nCr) =", findcomb(n, r))
Rahul Suresh 22IZ029

15. To Check Leap Year or not

Code:
year=int(input("Enter the year you want to check for leap year: "))
if (year % 400 == 0) and (year % 100 == 0):
print("{0} is a leap year".format(year))

elif (year % 4 ==0) and (year % 100 != 0):


print(year,"is a leap year")

else:
print(year,"is not a leap year")

Output:
Enter the year you want to check for leap year: 2020
2020 is a leap year
Rahul Suresh 22IZ029

16. To Reverse a Number

Code:
a=input("Enter any number other than single digit number: ")
print ("The reversed order of",a,"is",a[::-1])

Output:
Enter any number other than single digit number: 3567
7653
Rahul Suresh 22IZ029

17. To Check Whether a Number is Palindrome or Not

Code:
num=input("Enter a number to check for palindrome: ")
reverse = int(str(num)[::-1])
if num == reverse:
print('Palindrome')
else:
print("Not Palindrome")

Output:
Enter a number to check for palindrome: 1221
Palindrome
Rahul Suresh 22IZ029

18. To Make a Simple Calculator Using CASE

Code: Output:
def add(x,y): Enter a number: 4
return x+y Enter another number: 7
def multiply(x,y): 1-Add
return x*y 2-Subtract
def subtract(x,y): 3-Multiply
return x-y
4-Divide2
def divide(x,y):
Answer= -3
return x/y
a=int(input("Enter a number: "))
b=int(input("Enter another number: "))
x=int(input("1-Add\n2-Subtract\n3-Multiply\n4-Divide"))
if x==1:
ans=add(a,b)
elif x==2:
ans=subtract(a,b)
elif x==3:
ans=multiply(a,b)
elif x==4:
ans=divide(a,b)
else:
print("Enter correctly")
print("Answer=",ans)
Rahul Suresh 22IZ029

20. To Count Number of Digits in number

Code:
a=int(input("Enter a number: "))
s=str(a)
l=len(s)
print("Number of digits in the number is",l)

Output:
Enter a number: 6788
Number of digits in the number is 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy