Neel Sanjay Bhatt Roll No:-07 Div:A

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

Neel Sanjay Bhatt

Roll No:-07
Div:A

EXPERIMENT NO:4
AIM: Write a program to demonstrate use of control statements in python:

a) Write a program in python to implement FizzBuzz


b) A python program to display even numbers between m and n (m and n are taken from
user)
c) Write a python program to swap two numbers and find whether the first number is
positive or 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.

TOOLS USED: Python 3.4.3, Terminal


THEORY:

A. IF-ELIF-ELSE STATEMENT
The if-elif-else statement is used to check multiple conditions.There can be multiple elif test

conditions.

B.WHILE LOOP
The while loop is used for executing a block of code multiple times until a certain condition remains
true.Since the loop needs to terminate so the iteration or terminating statement is written inside the
body.
C.FOR LOOP
The for loop is similar to the while loop except that the for loop uses a sequence for the execution.

FIG:FO
R LOOP EXECUTION
a)Write a program in python to implement FizzBuzz
Code:
for i in range(1,21):
if i%3==0 and i%5==0:
print("FizzBuzz")
elif i%3==0:
print("Fizz")
elif i%5==0:
print("Buzz")
else:
print(i)

Output:
lab@lab:~/Desktop/A07$ python3 ppy.py
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
lab@lab:~/Desktop/A53$

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

Code:

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

print('Even numbers 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/A07$ python3 200.py

Enter starting and ending number 20 30

Even numbers are:

20

22

24

26

28

Total count is

5
c)Write a python program to swap two numbers and find whether the first number is
positive or negative or zero

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:
lab@lab:~/Desktop/A07$ python3 swap.py
Enter 2 no -3 4
After swaping:
4 -3
1st Number is Positive

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

Code:

x=[int(x) for x in input(‘enter list ’).split()]


key=int(input(‘enter element to search ’))
if key in x:
print(key,‘is found at’,x.index(key))
else:
print(“Not found”)
OUTPUT
lab@lab:~/Desktop/A07$ python3 search.py
enter list 1 2 3 4
enter element to search 2
2 is found at 1
CONCLUSION:
Thus, we have studied the use of control flow statements.

You might also like