Chandigarh University: Experiment 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Chandigarh University

Experiment 2
Aim : Program to demonstrate the use of if, if-else, while, for, break and continue

Discription:

An else statement can be combined with an if statement. An else statement contains the
block of code that executes if the conditional expression in the if statement resolves to 0
or a FALSE value.

The elif statement allows you to check multiple expressions for TRUE and execute a
block of code as soon as one of the conditions evaluates to TRUE.

Break and continue are used to manipulate the working of a loop. Break is used to end the
program when the condition is met and continue is used to skip the statements in the
current iteration after the continue keyword and starts the next iteration.

Program:

(a)write a program to print the days of the week

day=eval(input("Enter a no. from 1 to 7:\n"))

day=day%7

if(day==1):

print("Monday\n")

if(day==2):

print("Tuesday\n")

if(day==3):

print("Wednesday\n")

if(day==4):

print(" Thursday\n")

if(day==5):

print("Friday\n")

if(day==6):

print("Saturday\n")

if(day==7):

print("Sunday\n")

Ajesh Kumar Yadav UID:16BCS1030


Chandigarh University

(b)write a program to determineif a persomn is eligible to vote and if he is not


eligible then what is the age difference required for him to be eligible.

age=eval(input("Enter Your age:"))

if(age==18 or age>18):

print("eligible to vote:\n")

else:

print("Not eligible to vote:\n")

print(18-age," years left for eligibility")

(c)program to find wheather the number is positive or negative

x=int(input("Enter any number"))

if(x>0):

print("Number is positive")

elif(x==0):

print("Number is zero")

else:

print("Number is negative")

d) program to find the greatest of the three

x=int(input("Enter any number"))

y=int(input("Enter another number"))

z=int(input("Enter another number"))

if(x>y and x>z):

print(x, " is greatest")

elif(y>z and y>x):

print(y, " is greatest")

Ajesh Kumar Yadav UID:16BCS1030


Chandigarh University

else:

print(z," is greatest")

(e)Program to demonstrate the use of break and continue


for i in range(0,11):
if(i==6):
break
if(i==3):
continue
print(i)

Output:

Ajesh Kumar Yadav UID:16BCS1030

You might also like