Python if….
else Statement
1. WAP to calculate the electricity bill (accept number of unit from user ) according to the
following criteria:
UNIT PRICE
First 100 Units No Charge
Next 100 Units Rs 5 per unit
After 200 Units Rs 10 per unit
( For example if input unit is 350 than total bill amount is Rs 2000)
Source Code :
amt=0
num=int(input("Enter the number of electric unit"))
if num<=100:
amt=0
elif num>100 and num<=200:
amt=(num-100)* 5
elif num>200:
amt=500 +(num-200)* 10
print("Amt to pay", amt)
2. Write a program to display the last digit of a number.
(Hint : Any number % 10 will return the last digit.
Source Code :
num=int(input("Enter any number"))
print("last digit of number is ", num % 10)
3. Write a program to accept percentage from the user and display the grade according to the
following criteria:
Marks Grade
>90 A
>80 and <=90 B
>=60 and <=80 C
Below 60 D
per=int(input(“Enter your Percentage”))
if per>90:
print (“You get an A Grade”)
elif per>80 and per<=90:
print(“you got a B”)
elif per>=60 and per<=80:
print(“you got a C”)
else:
print (“you got a D”)
4. Write a program to accept the cost price of a bike and display the road tax to be paid
according to the following Criteria:
Cost Price (in Rs) Tax
>100000 15 %
>50000 and <=100000 10 %
<=50000 5%
tax=0
pr=int(input(“Enter the price of bike”))
if pr>100000:
tax = 15/100 * pr
elif pr>50000 and pr<=100000:
tax=10/100*pr
else:
tax=5/100*pr
print (“Tax to be paid”,tax)
5. WAP to check whether an year is leap year or not.
year=int(input("Enter year to be checked:"))
if(year%4==0 and year%100!=0 or year%400==0):
print("The year is a leap year!")
else:
print("The year isn't a leap year!")
6. Write a program to accept a number from 1 to 7 and display the name of the day like 1 for
Sunday, 2 for Monday and so on.
Num=int(input(“Enter any number between 1 to 7 :”))
If num==1:
print(‘Sunday”)
elif num==2:
print(“Monday”)
elif num==3:
print(“Tuesday”)
elif num==4:
print(“Wednesday”)
elif num==5:
print(“Thursday”)
elif num==6:
print(“Friday”)
elif num==7:
print(“Saturday”)
else:
print(“The number you entered is invalid”)
7. WAP to accept any city from the user and display monument of that city.
City Monuments
Delhi Red Fort
Agra Taj Mahal
Jaipur Jal Mahal
city=input("Enter a city")
if city=="Dehli":
print ("Red Fort")
elif city=="Agra":
print("Taj Mahal")
elif city== "Jaipur":
print("Jal Mahal")
else:
print ("No monuments")
8. Write a program to whether a number (accepted from user) is divisible by 2 and 3 both.
Num1=int(input(“Enter First Number”))
If num1%2==0 and num1 %3==0:
print(Number is divisible by 2 and 3 both “)
else:
print(Number is not divisible by 2 and 3 both “)
9. Write a program to find the largest number out of three numbers excepted from user.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
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)
10. Accept the following from the user and calculate the percentage of class attended:
a. Total number of working days
b. Total number of days for absent
After calculating percentage show that, if the percentage is less than 75, than student will
not be able to sit in exam.
wd=int(input(“Enter total number of working days”))
na=int(input(“Enter number of days absent"))
per=(nd-na)/nd*100
if per<75:
print(“You are not eligible for exams”)
else:
print(“You are eligible for writing exam”)
11. WAP to check if the number is an Armstrong number or not.( Armstrong number of 3 digits,
the sum of cubes of each digit is equal to the number itself. For example: 153, 370, 371, and
407.)
Eg 153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.
num = int(input("Enter a number: "))
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
12. Write a Python Program to check whether a given number is a palindrome
A palindrome number is a number that remains the same when digits are reversed.
For example, the number 12321 is a palindrome number, but 1451 is not a palindrome
number. Eg 16461 - Palindrome
n=int(input(“Enter Number”))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print(“The number is a palindrome”)
else:
print(“The number isn’t a palindrome”)