Python Tutorial: If Related Program in Python
Showing posts with label If Related Program in Python. Show all posts
Showing posts with label If Related Program in Python. Show all posts

Saturday, 25 July 2020

If Related Program in Python


List of if...else programming exercises

1.      Write a Python program to find maximum between two numbers.
Ans: n1=int(input('Enter the first number :'))
n2=int(input('Enter the second number :'))
if(n1>n2):
    print(n1, "is greater than ",n2)
else:
    print(n2, "is greater than ",n1)
Or
##Write a Python program to find maximum between two numbers.
n1=int(input('Enter the first number :'))
n2=int(input('Enter the second number :'))
if(n1==n2):
    print("Both are equal.")
elif(n1>n2):
    print(n1, "is greater than ",n2)
else:
    print(n2, "is greater than ",n1)

2.      Write a Python program to find maximum between three numbers.
Ans : ##Write a Python program to find maximum between three numbers.
a=int(input('Enter the first number :'))
b=int(input('Enter the second number :'))
c=int(input('Enter the third number :'))
if(a==b==c):
    print('These are equal.')
elif(a>b>c):
    print(a, ' is the biggest number.')
elif(b>a>c):
    print(b, ' is the biggest number.')
else:
    print(c, ' is the biggest number.')

3.      Write a Python program to check whether a number is negative, positive or zero.
Ans : #Write a Python program to check whether a number is negative, positive or zero.
num=int(input('Enter the number :'))
if(num==0):
    print('It is zero.')
elif(num>0):
    print('It is positive.')
else:
    print('It is negative.')

4.      Write a Python program to check whether a number is divisible by 5 and 11 or not.
Ans: num=int(input('Enter the number :'))
if(num%5==0 and num%11==0):
    print('It is divisible by 5 and 11')
else:
    print('It is not divisible by 5 and 11')

5.      Write a Python program to check whether a number is even or odd.
Ans : #Write a Python program to check whether a number is even or odd.
num=int(input('Enter the number :'))
if(num%2==0):
    print(num, ' is even number')
else:
    print(num, ' is odd number')

6.      Write a Python program to check whether a year is leap year or not.
Ans : year = int(input("Enter a year: "))
if (year % 4) == 0 and
   if (year % 100) == 0:
       if (year % 400) == 0:
           print(year , “ is a leap year")
       else:
           print(year , “ is not a leap year")
   else:
       print(year , “ is a leap year")
else:
   print(year , “ is not a leap year")
OR
year = int(input("Enter Year: "))
if (year % 4 == 0 and year % 100 != 0):
    print(year, "is a Leap Year")
elif (year % 100 == 0):
    print(year, "is not a Leap Year")
elif (year % 400 ==0):
    print(year, "is a Leap Year")
else:
    print(year, "is not a Leap Year")

7.      Write a Python program to check whether a character is alphabet or not.
Ans : ch = input("Please Enter Your Own Character : ")

if((ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')): 
    print("The Given Character ", ch, "is an Alphabet") 
elif(ch >= '0' and ch <= '9'):
    print("The Given Character ", ch, "is a Digit")
else:
    print("The Given Character ", ch, "is Not an Alphabet or a Digit")
                        OR
ch=input("Please enter the character as you wish")
if((ord(ch)>=65 and ord(ch)<=90)or(ord(ch)>=97 and ord(ch)<=122)):
    print(ch," is an Alphabet: ");
else:
    print(ch," is not an Alphabet: ")
else:
    print("The Given Character ", ch, "is Not an Alphabet or a Digit")
OR
ch=input("Please enter the character as you wish")
if((ord(ch)>=65 and ord(ch)<=90)or(ord(ch)>=97 and ord(ch)<=122)):
    print(ch," is an Alphabet: ");
else:
    print(ch," is not an Alphabet: ")
8.      Write a Python program to input any alphabet and check whether it is vowel or consonant.
Ans :  CH = input("Input a letter of the alphabet: ")
if CH.lower() in ('a', 'e', 'i', 'o', 'u'):
   print("%s is a vowel." % CH)
else:
   print("%s is a consonant." % CH)
OR
ch = input("Please Enter Your Own Character : ")
if(ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u' or ch == 'A' or ch == 'E' or ch == 'I' or ch == 'O' or ch == 'U'):
    print("The Given Character ", ch, "is a Vowel")
else:
    print("The Given Character ", ch, "is a Consonant")
OR
ch = input("Please Enter Your Own Character : ")

if(ord(ch) == 65 or ord(ch) == 69 or ord(ch) == 73
       or ord(ch) == 79 or ord(ch) == 85
       or ord(ch) == 97 or ord(ch) == 101 or ord(ch) == 105
       or ord(ch) == 111 or ord(ch) == 117):
    print("The Given Character ", ch, "is a Vowel")
elif((ord(ch) >= 97 and ord(ch) <= 122) or (ord(ch) >= 65 and ord(ch) <= 90)):
    print("The Given Character ", ch, "is a Consonant")
else:
    print("Wrong input.")
OR
l = input("Enter the character: ")
if l.lower() in ('a', 'e', 'i', 'o', 'u'):
  print("Vowel")
elif l.upper() in ('A', 'E', 'I', 'O', 'U'):
  print("Vowel")
else:
  print("Consonant")

9.      Write a Python program to input any character and check whether it is alphabet, digit or special character.
Ans : ch = input("Please Enter Your Own Character : ")
if((ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')):
    print("The Given Character ", ch, "is an Alphabet")
elif(ch >= '0' and ch <= '9'):
    print("The Given Character ", ch, "is a Digit")
else:
    print("It is special Symbol or character")

10.  Write a Python program to check whether a character is uppercase or lowercase alphabet.
Ans : ch = input("Please Enter Your Own Character : ")
if(ch >= 'a' and ch <= 'z'):
   print("The Given Character ", ch, "is an lower character")
elif(ch >= 'A' and ch <= 'Z'):
    print("The Given Character ", ch, "is an upper character")  
elif(ch >= '0' and ch <= '9'):
    print("The Given value ", ch, "is a Digit")
else:
    print("The Given Character ", ch, "is special character")

11.  Write a Python program to input week number and print week day.
weekday = int(input("Enter weekday day number (1-7) : "))
if weekday == 1 :
    print("Monday")
elif weekday == 2 :
    print("Tuesday")
elif(weekday == 3) :
    print("Wednesday")
elif(weekday == 4) :
    print("Thursday")
elif(weekday == 5) :
    print("Friday")
elif(weekday == 6) :
    print("Saturday")
elif (weekday == 7) :
    print("Sunday")
else :
    print("Please enter weekday number between 1-7.")

12.                   Write a Python program to convert month name to a number of days.
Ans : monthName = input("Input the name of Month: ")
if (monthName == "February"):
   print("No. of days: 28/29 days")
elif monthName in ("April", "June", "September", "November"):
   print("No. of days: 30 days")
elif monthName in ("January", "March", "May", "July", "August", "October", "December"):
   print("No. of days: 31 day")
else:
   print("Wrong month name")

13.  Write a Python program to input month number and print number of days in that month.
Ans : month=int(input("Enter the month number"))
if(month==1 or month==3 or month==5 or month==8 or month==12 or month==10):
  numd=31
  print("Number of days in this month is",numd)
if(month==2):
  numd=28
  numd1=29
  print("Number of days in this month is",numd,"or",numd1)
if(month==4 or month==6 or month==7 or month==9 or month==11 ):
  numd=30
  print("Number of days in this month is",numd)

14.  Write a Python program to count total number of notes in given amount.
Ans: amount=int(input("Enter an amount"))
n500=n200=n100=n50=n20=n10=n5=n2=n1=0
if(amount>=500):
    n500=amount//500   
    amount=amount-n500*500
if(amount>=200):
    n200=amount//200   
    amount=amount-n200*200
if(amount>=100):
    n100=amount//100   
    amount=amount-n100*100
if(amount>=50):
    n50=amount//50   
    amount=amount-n50*50
if(amount>=20):
    n20=amount//20   
    amount=amount-n20*20
if(amount>=10):
    n10=amount//10  
    amount=amount-n10*10
if(amount>=5):
    n5=amount//5   
    amount=amount-n5*5
if(amount>=2):
    n2=amount//2   
    amount=amount-n2*2
if(amount>=1):
    n1=amount//1   
    amount=amount-n1*1  
print("Rs. 500 =",n500," Notes")
print("Rs. 200 =",n200," Notes")
print("Rs. 100 =",n100," Notes")
print("Rs. 50 =",n50," Notes")
print("Rs. 20 =",n20," Notes")
print("Rs. 10 =",n10," Notes")
print("Rs. 5 =",n5," Notes")
print("Rs. 2 =",n2," Notes")
print("Rs. 1 =",n1," Notes")   
   
15.  Write a Python program to input angles of a triangle and check whether triangle is valid or not.
Ans: 
angle1=int(input("Enter the first angle :"))
angle2=int(input("Enter the Second angle :"))
angle3=int(input("Enter the Third angle :"))
if((angle1+angle2+angle3)==180):
    print('Triangle is valid.')
else:
    print('Triangle is valid.')
   
16.  Write a Python program to input all sides of a triangle and check whether triangle is valid or not. 
Ans : # Python Program to Check Whether a Triangle is Valid or Not

a = float(input("Enter first side: "))
b = float(input("Enter second side: "))
c = float(input("Enter third side: "))

if (a + b > c) and (a + c > b) and (b + c > a):
    print("The triangle is Valid.")
else:
    print("The triangle is Invalid.")
17.  Write a Python program to check whether the triangle is equilateral, isosceles or scalene triangle.
Ans : # Python Program to Check the Type of Triangle

a = float(input("Enter first side: "))
b = float(input("Enter second side: "))
c = float(input("Enter third side: "))

# Check if the triangle is valid
if (a + b > c) and (a + c > b) and (b + c > a):
    if a == b == c:
        print("The triangle is Equilateral.")
    elif a == b or b == c or a == c:
        print("The triangle is Isosceles.")
    else:
        print("The triangle is Scalene.")
else:
    print("Invalid triangle.")
18.  Write a Python program to input marks of five subjects English, Physics, Chemistry, Mathematics, Computer. Calculate percentage and grade according to following:

Percentage >= 90% : Grade A

Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F

Ans : English=int(input("Enter the English Marks : "))
Physics=int(input("Enter the Physics Marks : "))
Chemistry=int(input("Enter the Chemistry Marks : "))
Mathematics=int(input("Enter the Mathematics Marks : "))
Computer=int(input("Enter the Computer Marks : "))
Total=English+Physics+Chemistry+Mathematics+Computer
Percentage=Total//5
print("Percentage=",Percentage)
if(Percentage >= 90):
    print("Grade A")
elif(Percentage >= 80):
    print("Grade B")
elif(Percentage >= 70):
    print("Grade C")
elif(Percentage >=60):
    print("Grade D")
elif(Percentage >= 40):
    print("Grade E")
else:
    print("Grade F")
   
19.  Write a Python program to input basic salary of an employee and calculate its Gross salary according to following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%

Ans : basic=int(input("Enter the Basic Salary :"))
hra=da=totalsalary=0
if(basic<=10000):
    hra=basic*0.20
    da=basic*0.80
elif(basic<=20000):
    hra=basic*0.25
    da=basic*0.90
else:
    hra=basic*0.30
    da=basic*0.95
totalsalary=basic+hra+da
print("================================")
print("    Employee Salary Detail     ")
print("================================")
print('Basic Salary         :',basic)
print('House Rent Allowance :',hra)
print('Dearness Allowance   :',da)
print('Total Salary         :',totalsalary)

20.  Write a Python program to input electricity unit charges and calculate total electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill

Ans : #Electricity bill
unit=int(input("Enter the Unit :"))
if(unit<=50):
    amt=unit*0.50
elif(unit<=150):
    amt=25+(unit-50)*0.75
elif(unit<=250):
    amt=100+(unit-150)*1.20
else:
    amt=220+(unit-250)*1.50
sur_charge=amt*0.20
total_amt=amt+sur_charge
print("Electricity bill ",total_amt)

21.  Write a Python program inputs three numbers and calculates two sums as per this:
Sum1 as the sum of all input numbers
Sum2 as the sum of non-duplicate numbers; if there are duplicate numbers in the input, ignores them
e.g. input of numbers 2,3,4 will give two sums as 9 and 9
  input of numbers 3,2,3 will give two sums as 8 and 2 (both 3’s ignored for second sum)
  input of numbers 4,4,4 will give two sums as 12 and 0 (all 4’s ignored for second sum)
Ans: n1=int(input("Enter Number 1 : "))
n2=int(input("Enter Number 2 : "))
n3=int(input("Enter Number 3 : "))
sum1=sum2=0
sum1=n1+n2+n3
if (n1!=n2 and n1!=n3):
    sum2=sum2+n1
if (n2!=n1 and n2!=n3):
    sum2=sum2+n2
if (n3!=n1 and n3!=n2):
    sum2=sum2+n3
print("Numbers are :",n1,n2,n3)
print("Sum of three given numbers is :",sum1)
print("Sum of non-duplicate numbers is :",sum2)
   
OR
        n1=int(input("Enter Number 1 :"))
n2=int(input("Enter Number 2 :"))
n3=int(input("Enter Number 3 :"))
sum1=sum2=0
sum1=n1+n2+n3
if (n1==n2):
    if(n3!=n1):
        sum2=sum2+n3
else:
    if (n1==n3):
        sum2=sum2+n2
    else:
        if(n2==n3):
            sum2=sum2+n1
        else:
            sum2=sum2+n1+n2+n3
print("Numbers are :",n1,n2,n3)
print("Sum of three given numbers is :",sum1)
print("Sum of non-duplicate numbers is :",sum2)

22.  Program that reads two numbers and an arithmetic operator and display the computed result.
Ans : n1=float(input("Enter the first number :"))
n2=float(input("Enter the second number :"))
op=input("Enter opertor [+,-,*,/,%]")
result=0
if(op=='+'):
    result=n1+n2
elif(op=='-'):
    result=n1-n2
elif(op=='*'):
    result=n1*n2
elif(op=='/'):
    result=n1/n2
elif(op=='%'):
    result=n1%n2
else:
    print("Invalid operator!!")
print(n1,op,n2,'=',result)

23.  Write a program to accept three numbers from user and print them in ascending and descending order.
Ans : a = int(input( "Enter a number >> "))
b = int(input( "Enter a second number >> "))
c = int(input( "Enter a third number >> "))
if((a>=b)and(a>=c)):
    if(b>=c):
        print("\n Descending order :   ",a,b,c)
        print("\n Ascending order :   ",c,b,a)
    else:
        print("\n Descending order :   ",a,c,b)
        print("\n Ascending order :   ",b,c,a)
elif((b>=a)and(b>=c)):
    if(a>=c):   
        print("\n Descending order :   ",b,a,c)
        print("\n Ascending order :   ",c,a,b)   
    else:   
        print("\n Descending order :   ",b,c,a)
        print("\n Ascending order :   ",a,c,b)
elif((c>=a)and(c>=b)):
    if(a>=b):
        print("\n Descending order :   ",c,a,b)
        print("\n Ascending order :   ",b,a,c)
    else:
        print("\n Descending order :   ",c,b,a)
        print("\n Ascending order :   ",a,b,c)

24.  Write a program to accept three numbers from user and print them in ascending.
Ans: a = int(input( "Enter a number >> "))
b = int(input( "Enter a second number >> "))
c = int(input( "Enter a third number >> "))
if(a<b and a<c):
    if(b<c):
        print("\n Ascending order :   ",a,b,c)
    else:
        print("\n Ascending order :   ",a,c,b)
elif(b<a and b<c):
    if(a<c):
        print("\n Ascending order :   ",b,a,c)
    else:
        print("\n Ascending order :   ",a,c,b)
else:
    if(a<b):
        print("\n Ascending order :   ",c,a,b)
    else:
        print("\n Ascending order :   ",c,b,a)

OR

a=int(input("Enter first number :"))
b=int(input("Enter second number :"))
c=int(input("Enter third number :"))
if a<b<c:
    f,s,t=a,b,c
elif a<c<b:
    f,s,t=a,c,b
elif b<a<c:
    f,s,t=b,a,c
elif b<c<a:
    f,s,t=b,c,a
elif c<a<b:
    f,s,t=c,a,b
elif c<b<a:
    f,s,t=c,b,a
print("Ascending Order : ",f,s,t,sep=',')