Selective Statements: In this program, some portion of the
program is executed based upon the
conditional test. If the conditional test is true, compiler
will execute some part of the program, otherwise it
will execute other part of the program. This is implemented
in python using if statement.
Syntax:
if (condition): if (condition):
statements statements
else (or) elif (condition):
statements statements
else:
Statements
Example:
1. Program to find the simple interest based upon number of
years. If number of years is more than 12 rate of interest is 10 otherwise 15.
Code:
p = input("Enter any principle amount")
t = input("Enter any time")
if (t>10):
si = p*t*10/100
else:
si = p*t*15/100
print "Simple Interest = ",si
output:
Enter any principle amount 3000
Enter any time12
Simple Interest = 3600
2. Write a program to input any choice and to implement the
following.
Choice Find
1. Area of square
2. Area of rectangle
3. Area of triangle
Code:
c = input ("Enter any Choice")
if(c==1):
s = input("enter
any side of the square")
a = s*s
print"Area =
",a
elif(c==2):
l = input("enter
length")
b = input("enter
breadth")
a = l*b
print"Area =
",a
elif(c==3):
x = input("enter
first side of triangle")
y = input("enter
second side of triangle")
z = input("enter
third side of triangle")
s = (x+y+z)/2
A =
((s-x)*(s-y)*(s-z))**0.5
print"Area=",A
else:
print "Wrong
input"
Output:
Enter any Choice2
enter length4
enter breadth6
Area = 24
No comments:
Post a Comment