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

If..else Program CLASS XII COMPUTER SCIENCE

Uploaded by

Dinesh Kumar Ram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

If..else Program CLASS XII COMPUTER SCIENCE

Uploaded by

Dinesh Kumar Ram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

if…else python program

1. Aim:- Write a program to find minimum number between two numbers :

n1=int(input("Enter first number:"))


n2=int(input("Enter second number:"))
if n1<n2:
print(n1,"is the minimum number.")
else:
print(n2,"is the minimum number.")

Output:--
>>>
Enter first number:5
Enter second number:2
2 is the minimum number.

2. Aim:- Write a program to find minimum number between two numbers :

n1=int(input("Enter first number:"))


n2=int(input("Enter second number:"))
n3=int(input("Enter third number:"))
if n1<n2 and n1<n3:
print(n1,"is the minimum number.")
elif n2<n3 and n2<n1:
print(n2,"is the minimum number.")
else:
print(n3,"is the minimum number.")
Output:--
>>>
Enter first number:10
Enter second number:15
Enter third number:9
9 is the minimum number.

3. Aim: Write a program to check whether a number is negative, positive or zero :

n=int(input("Enter any number:"))


if n<0:
print(n,"is a negative number.")
elif n>0:
print(n,"is a positive number.")
else:
print(n,"is a zero.")

Output:--
>>>
Enter any number:-1
-1 is a negative number.
>>>
1 is a positive number.
>>>
0 is a zero.

4. Aim: Write a program to check whether a number is divisible by 5 and 11 or not:

n=int(input("Enter any number:"))


if n%5==0 and n%11==0:
print(n,"is divisible by 5 and 11.")
else:
print(n,"is not divisible by 5 and 11.")
Output:--
>>>
Enter any number:1
13 is not divisible by 5 and 11.
>>>
Enter any number:1
55 is divisible by 5 and 11.

5. Aim: Write a program to check whether a number is even or odd:

n=int(input("Enter any number:"))


if n%2==0:
print(n,"is even number.")
else:
print(n,"is odd number.")

Output:--
>>>
Enter any number:5
5 is odd number.
>>>
Enter any number:8
8 is even number.
6. Aim: Write a program to check whether a year is a leap year or not:

year=int(input("Enter any year:"))


if year%100==0:
if year%400==0:
print(year,"is a leap year")
else:
print(year,"is not a leap year")
else:
if year%4==0:
print(year,"is a leap year")
else:
print(year,"is not a leap year")
output:--
>>>
Enter any year:2020
2020 is a leap year
>>>
Enter any year:2003
2003 is not a leap year.

7. Aim: Write a program to check whether a character is alphabet or not:

ch=input("Enter any character:")


if (ch>='a' and ch<='z') or (ch>='A' and ch<='Z'):
print(ch,"is an alphabet.")
else:
print(ch,"is not an alphabet.")

Output:--
>>>
Enter any character:s
s is an alphabet.
>>>
Enter any character:5
5 is not an alphabet.

8. Aim: Write a program to input any alphabet and check whether it is vowel or consonant:

ch=input("Enter any alphabet:")


if ch in ['a','e','i','o','u','A','E','I','O','U']:
print(ch,"is vowel.")
else:
print(ch,"is a consonant.")

Output:--
>>>
Enter any alphabet:g
g is a consonant.
>>>
Enter any alphabet:e
e is vowel

9. Aim: Write a program to input any character and check whether it is alphabet, digit or
special character:

ch=input("Enter any character:")


if (ch>='a' and ch<='z') or (ch>='A' and ch<='Z'):
print(ch,"is an alphabet.")
elif ch>='0' and ch<='9':
print(ch,"is a digit.")
else:
print(ch,"is a special character.")
output:--
>>>
Enter any character:j
j is an alphabet.
>>>
Enter any character:8
8 is a digit.
>>>
Enter any character:&
& is a special character.

10. Aim: Write a program to check whether a character is uppercase or lowercase alphabet:
ch=input("Enter any character:")

if (ch>='a' and ch<='z') :


print(ch,"is a lowercase alphabet.")
elif (ch>='A' and ch<='Z'):
print(ch,"is a uppercase alphabet.")
else:
print(ch,"is not a valid alphabet.")

Output:--
>>>
Enter any character:f
f is a lowercase alphabet.
>>>
Enter any character:J
J is an uppercase alphabet.
11. Aim: Write a program to input week number and print week day.

n=int(input("Enter a week number:"))


if n==1:
print(n,"is sunday.")
elif n==2:
print(n,"is monday.")
elif n==3:
print(n,"is tuesday.")
elif n==4:
print(n,"is wednesday.")
elif n==5:
print(n,"is thursday.")
elif n==6:
print(n,"is friday.")
elif n==7:
print(n,"is saturday.")
else:
print("input number between 1-7.")

output:--
>>>
Enter a week number:6
6 is friday.
Enter a week number:5
5 is thursday.
>>>
Enter a week number:7
7 is saturday.
>>>
Enter a week number:1
1 is sunday
12. Aim: Write a program to input month number and print number of days in month:

n=int(input("Enter a month number:"))


if n==1:
print("No. of days in this month is 31")
elif n==2:
print("No. of days in this month is 28")
elif n==3:
print("No. of days in this month is 31")
elif n==4:
print("No. of days in this month is 30")
elif n==5:
print("No. of days in this month is 31")
elif n==6:
print("No. of days in this month is 30")
elif n==7:
print("No. of days in this month is 31")
elif n==8:
print("No. of days in this month is 30")
elif n==9:
print("No. of days in this month is 30")
elif n==10:
print("No. of days in this month is 31")
elif n==11:
print("No. of days in this month is 30")
else:
print("No. of days in this month is 31")
output:--
>>>
Enter a month number:5
No. of days in this month is 31
>>>
Enter a month number:7
No. of days in this month is 31

13. Aim: Write a program to input angles of a triangle and check whether triangle is valid or
not:
n1=int(input("Enter first angle:"))
n2=int(input("Enter second angle:"))
n3=int(input("Enter third angle:"))
sum=n1+n2+n3
if sum==180:
print("Triangle is valid.")
else:
print("Triangle is not valid.")

Output:--
>>>
Enter first angle:60
Enter second angle:60
Enter third angle:60
Triangle is valid.
>>>
Enter first angle:90
Enter second angle:45
Enter third angle:80
Triangle is not valid.
14. Aim: Write a program to input all sides of a triangle and check whether triangle is valid
or not.

n1=int(input("Enter first angle:"))


n2=int(input("Enter second angle:"))
n3=int(input("Enter third angle:"))
if (n1*n1)==(n2*n2)+(n3*n3):
print("Triangle is valid.")
elif (n2*n2)==(n3*n3)+(n1*n1):
print("Triangle is valid.")
elif (n3*n3)==(n1*n1)+(n2*n2):
print("Triangle is valid.")
else:
print("Triangle is not valid.")

output:--
>>>
Enter first angle:3
Enter second angle:4
Enter third angle:5
Triangle is valid.

15. Aim: Write a program to check whether the triangle is equilateral, isosceles or scalene
triangle.

n1=int(input("Enter first angle:"))


n2=int(input("Enter second angle:"))
n3=int(input("Enter third angle:"))
if n1==n2==n3:
print("Triangle is an equilateral.")
elif n1==n2 or n2==n3 or n1==n3:
print("Triangle is an isosceles.")
else:
print("Triangle is scalene.")
output:--
Enter first angle:5
Enter second angle:5
Enter third angle:3
Triangle is an isosceles.
>>>
Enter first angle:6
Enter second angle:6
Enter third angle:6
Triangle is an equilateral.

>>> Enter first angle:3


Enter second angle:4
Enter third angle:5
Triangle is scalene.

16. Aim: Write a program to find all roots of a quadratic equation.

import math
import math
print("For quadratic equation, ax**2+bx+c=0, enter coefficient below")
a=int(input("Enter a:"))
b=int(input("Enter b:"))
c=int(input("Enter c:"))
delta=b*b-4*a*c
if a==0:
print("Error")
print("Value of a should not be zero")
else:
if delta>0:
root1=(-b+math.sqrt(delta))/(2*a)
root2=(-b-math.sqrt(delta))/(2*a)
print("Roots are Real and Unequal")
print("Roots are:",root1,'and',root2)
elif delta==0:
root=-b/(2*a)
print("Roots are Real and Equal")
print("Roots are:",root1,'and',root2)
else:
print("Roots are complex and imaginery")

output:--
>>>
For quadratic equation, ax**2+bx+c=0, enter coefficient below
Enter a:1
Enter b:5
Enter c:2
Roots are Real and Unequal
Roots are: -0.4384471871911697 and -4.561552812808831

17. Aim: Write a program to calculate profit or loss.

cp=float(input("Enter cost price:"))


sp=float(input("Enter selling price:"))
if cp>sp:
print("Loss")
elif sp>cp:
print("Profit")
else:
print("No profit no loss!!")

output:--
>>>
Enter cost price:20
Enter selling price:25
Profit
>>>
Enter cost price:10
Enter selling price:8
Loss
>>>
Enter cost price:15
Enter selling price:15
No profit no loss!

18. Aim: Write a program to input marks of five subjects Physics, Chemistry,
Biology mathematics and 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

phy=int(input("Enter physics marks:"))

chem=int(input("Enter chemistry marks:"))

bio=int(input("Enter biology marks:"))

maths=int(input("Enter maths marks:"))

comp=int(input("Enter computer marks:"))

marks=phy+chem+bio+maths+comp

perc=(marks/500)*100

if perc>=90 and perc<=100:

print("Percentage=",perc,"and Grade is A")


elif perc>=80 and perc<90:

print("Percentage=",perc,"and Grade is B")

elif perc>=70 and perc<80:

print("Percentage=",perc,"and Grade is C")

elif perc>=60 and perc<70:

print("Percentage=",perc,"and Grade is D")

elif perc>=40 and perc<60:

print("Percentage=",perc,"and Grade is E")

else:

print("Percentage=",perc,"and Grade is F")

output:--

>>> Enter physics marks:40

Enter chemistry marks:50

Enter biology marks:60

Enter maths marks:70

Enter computer marks:80

Percentage= 60.0 and Grade is D

>>>

Enter physics marks:70

Enter chemistry marks:70

Enter biology marks:80

Enter maths marks:85

Enter computer marks:95


Percentage= 80.0 and Grade is B

>>> Enter physics marks:90

Enter chemistry marks:90

Enter biology marks:90

Enter maths marks:90

Enter computer marks:.90

Percentage= 90.0 and Grade is A

19. Write a 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 > 10000 : HRA=30% , DA=95%

basic_sal=int(input("Enter basic salary:"))


if basic_sal>20000:
HRA=basic_sal*(30/100)
DA=basic_sal*(95/100)
gross_sal=basic_sal+HRA+DA
print(gross_sal)
elif basic_sal<=20000 and basic_sal>10000:
HRA=basic_sal*(25/100)
DA=basic_sal*(90/100)
gross_sal=basic_sal+HRA+DA
print(gross_sal)
else:
HRA=basic_sal*(20/100)
DA=basic_sal*(80/100)
gross_sal=basic_sal+HRA+DA
print(gross_sal)
output:--
>>>
Enter basic salary:10000
20000.0
>>>
Enter basic salary:22000
49500.0
>>>
Enter basic salary:8000
16000

20. Aim: Write a programe to iput electricity unit charges and calculate bill according to the
given condition:

For first 50 units Rs 0.50/unit


For next 50 units Rs 0.75/unit
For next 100 units Rs 1.20/unit
For unit above 250 units Rs 1.50/unit
An additional surcharge of 20% is added to the bill.
units = int(input(" Please enter Number of Units you Consumed : "))
if(units < 50):
amount = units * 0.50
surcharge = (20/100)*amount
elif(units <= 100):
amount = units * 0.75
surcharge = (20/100)*amount
elif(units <= 200):
amount = units * 1.20
surcharge = (20/100)*amount
else:
amount = units * 1.50
surcharge = (20/100)*amount
total = amount + surcharge
print("Total Electricity Bill:",total)
output:---
>>>
Please enter Number of Units you Consumed : 45
Total Electricity Bill: 27.0
>>>
Please enter Number of Units you Consumed : 85
Total Electricity Bill: 76.5
>>>
Please enter Number of Units you Consumed : 500
Total Electricity Bill: 900.0

You might also like