Q.
1) Write a program to print the reverse of a number
Answer 1: num= int(input("Enter a number : "))
rev=0
while (num>0):
a = num%10 #get remainder
rev = rev*10 + a
num=num//10
print("The reversed number is:",rev)
Q.2) Write a program to find whether given number is an Armstrong or not.
Answer 2: # Python Program to check Armstrong Number Using While Loop
Number = int(input("Please Enter the Number to Check: "))
Sum = 0
Times = 0
Temp = Number
while Temp > 0:
Times = Times + 1
Temp = Temp // 10
Temp = Number
while Temp > 0:
Reminder = Temp % 10
Sum = Sum + (Reminder ** Times)
Temp //= 10
if Number == Sum:
print("%d is Armstrong." %Number)
else:
print("%d is Not Armstrong." %Number)
Q.3) WAP to calculate roots of Quadratic Equation
Answer : #WAP to calculate roots of Quadratic Equation
a=int(input("Enter the value of a: "))
b=int(input("Enter the value of b: "))
c=int(input("Enter the value of c: "))
D=(b*b)-(4*a*c)
deno = 2*a
if(D>0):
print("Real Roots")
root1=(-b+D**0.5)/deno
root2=(-b-D**0.5)/deno
print("Root1=",root1,"\t Root2=",root2)
elif(D==0):
print("Equal Roots")
root1= -b/deno
print("Root1 and Root2=", root1)
else:
print("Imaginary Roots")
Q.4) Write a program to find the greatest number from three numbers
Answer : num1 = int(input("enter 1st no : "))
num2 = int(input("enter 2nd no : "))
num3 = int(input("enter 3rd no : "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
Q.5) Write a Program whether a given year is leap year or not.
Answer : year = int(input("Enter a 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("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
Q.6) Write a program to determine whether a person is eligible to vote.
Answer : # Python program to check vote eligibility
age = int(input("Enter age of a user: "))
if age >= 18:
print("User is eligible for voting: ", age)
else:
print("User is not eligible for voting: ", age)
Q.7) Write a program to determine whether a person is eligible to vote or not. If
not eligible, display how many years are left to be eligible.
Answer: age = int(input("Enter age of a user: "))
if age >= 18:
print("User is eligible for voting: ", age)
elif age<=18:
years_left=18-age
print("user is not eligible to vote")
print("years left to be eligible :",years_left)
Q.8) Write a program to determine the character entered by the user
Answer : # Python Program to check character is Alphabet Digit or Special Character
character = input("Please Enter Your Own Character : ")
if(character.isdigit()):
print("The Given Character ", character, "is a Digit")
elif(character.isalpha()):
print("The Given Character ", character, "is an Alphabet")
else:
print("The Given Character ", character, "is a Special Character")
Q.9) WAP to convert degreen fahrenheit into degrees celcius.
#Fahrenheit (F) to Celsius (C): C = 5/9 x (F-32)
Answer : fahrenheit = float(input("enter temprature in fahreneit : "))
celsius = (fahrenheit - 32)/1.8
print(str(fahrenheit )+ " degree Fahrenheit is equal to " + str(celsius ) + "
degree Celsius." )
Q.10) Write a program to calculate area of a triangle using Heron's Formula.
#(Hint: Heron's formula - area = sqrt(S*(s-a)*(s-b)*(s-c)))
Answer : # Python Program to find the area of triangle
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
Q.11) div 3 and 2
div by 2 not by 3
div by 3 not by 2
neigher by 2 nor ny 3
Answer: lower = int(input("Enter lower range limit:"))
upper = int(input("Enter upper range limit:"))
print("python program to print all the numbers divisible by 3 and 2 −")
for i in range(lower, upper+1):
if((i%3==0) & (i%2==0)):
print(i)
print("python program to print all the numbers divisible by 2 and not by 3 −")
for i in range(lower, upper+1):
if((i%2==0) & (i%3!=0)):
print(i)
print("python program to print all the numbers divisible by 3 and not by 2 −")
for i in range(lower, upper+1):
if((i%3==0) & (i%2!=0)):
print(i)
print("python program to print all the numbers not divisible by 3 and 2 −")
for i in range(lower, upper+1):
if((i%3!=0) & (i%2!=0)):
print(i)
Q.12) if amount is less than 1000, get discount 5%, otherwise 10% discount a person
get. Find Net payable.
#net payable = amt - dis
Answer : # input sale amount
amt = float(input("Enter Sale Amount: "))
# checking conditions and calculating discount
if(amt<1000):
disc = amt*0.05
print("Discount : ",disc)
print("Net Pay : ",amt-disc)
elif(amt>=1000):
disc=amt*0.10
print("Discount : ",disc)
print("Net Pay : ",amt-disc)
else:
print("Invalid Amount")
Q.13) Create a program which calculates the discount and the Net payable amount.
Protocol: if amount is less than 1000 then 5% of discount is provided; else if the
amount is between 1000 to 5000 10% of discount. For amounts more than 5000 15% of
discount is given.
Answer : # input sale amount
amt = float(input("Enter Sale Amount: "))
# checking conditions and calculating discount
if(amt<1000):
disc = amt*0.05
print("Discount : ",disc)
print("Net Pay : ",amt-disc)
elif(1000<=amt<5000):
disc=amt*0.10
print("Discount : ",disc)
print("Net Pay : ",amt-disc)
elif(amt>=5000):
disc=amt*0.15
print("Discount : ",disc)
print("Net Pay : ",amt-disc)
else:
print("Invalid Amount")