You are on page 1of 8

Name:Pratiksha D.

Laldas

SE comps A

Roll No-53

Experiment No:- 04

Aim: Write a program to demonstrate use of control statements in python.

a) Write a program in python to implement FizzBizz.

Theory:

In this program,if and elif control statements

For each number divisible by three the computer will display the word “fizz”,

For each number divisible by five the computer will display the word “bizz”,

For each number divisible by three and by five the computer will display the word “fizz-bizz”,

We use for loop to itterate an range function to specify starting and ending number.

Flowchart:
Code:

for i in range(1,21):

if i%3==0 and i%5==0:

print('Fizz BiZZ')

elif i%5==0:

print('bizz')

elif i%3==0:

print('fizz')

else:

print(i)

Output:
student@lab:~/Desktop/A53$ python3 range.py

fizz

bizz

fizz

fizz

bizz

11

fizz

13

14

Fizz BiZZ

16

Conclusion:Hence we implemented FizzBizz program using if and elif control statements.

b)A python program to display even number between m and n(m and n are taken from user).

Theory:

Here we ask the user to enter the starting and ending value,we print all the even numbers between the
mentioned range.

we use while loop for itteration and modulus operator and if control statement for calculating if the number is
even or not.

Flowchart:
Code:

x,y=[int(x) for x in input("Enter starting and ending number ").split(" ")]

print('Even nymbers are: ')

count=0

while range(x,y):

if x%2==0:

print(x)

count+=1

x+=1

print("Total count is")

print(count)

Output:-

student@lab:~/Desktop/A53$ python3 200.py

Enter starting and ending number 20 30


Even numbers are:

20

22

24

26

28

Total count is

Conclusion: Hence we successfully displayed even numbers between mentioned range using while loop and if
control statement.

c) Write a python program to swap two numbers and find whether the first number is positive,Negative
equal to zero.

Theory:

In this program we ask for 2 numbers from user and swap them,we then check if -

First number is positive

First number is negative

First number is equal to zero

using if and elif control statement.

Flowchart:
Code:

x,y=[int(x) for x in input("Enter 2 no ").split(" ")]

x,y=y,x

print("After swaping")

print(x,y)

if x>0:

print("1st Number is Positive")

elif x<0:

print("1st Number is Negative")

else:

print("1st Number is zero")

OUTPUT:

student@lab:~/Desktop/A53$ python3 swap.py

Enter 2 no 8 9

After swaping
98

1st Number is Positive

Conclusion- Hence we successfully swapped 2 numbers and checked if the number is positive,negative or equal
to zero.

d) A python program to search for an element in the list of elements(The list must be entered through
keyboard).

Theory:

In this program we search for and element in the list,list is taken from the user.

we can use either while or for loop for itteration.

we have used if control statement for comparision between key to be searched and elements of array.

we array element and key are equal we display "key found at location"

else display "Key not found".

Flowchart:

Code:

c=0

a=[int(x) for x in input('enter a list\t').split()]


n=int(input('enter no. to be searched '))

for i in a :

if i==n:

c+=1

m=i-1

if c==0:

print('no. not found')

else:

print("no found at ",m)

OUTPUT:-

student@lab:~/Desktop/A53$ python3 list.py

enter a list 2 5 6 7 8 9

enter no. to be searched 3

no. not found

Conclusion:

Hence we successfully implemented the program using for loop and if control statement.

You might also like