Python Exercise-5
Python Exercise-5
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
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
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
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
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
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
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
Output:
Enter 1st number: 5
Enter 2nd number: 3
Enter 3rd number: 15
5 is the second largest number
Rahul Suresh 22IZ029
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
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
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
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
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
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))
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
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
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
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
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