0% found this document useful (0 votes)
30 views4 pages

Chapter - 6 (Flow of Control)

Uploaded by

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

Chapter - 6 (Flow of Control)

Uploaded by

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

Chapter – 6(Flow of Control)

Introduction
The order of execution of the statements in a program is known as flow of control. The
flow of control can be implemented using control structures. Python supports two types
of control structures—selection and repetition.

(1) Write a program to find the difference of two numbers.


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
difference = num1 - num2
print("The difference of two number is", difference)

Selection : Selection means to select a specific condition according to requirement. For


doing we can use if – else statement.
Syntax : if condition:
statement(s)
else:
statement(s)

(2) Write a code to find the voting eligibility of the candidate.


age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")

Important Note : Many a times there are situations that require multiple conditions to
be checked and it may lead to many alternatives. In such cases we can change the
conditions using if..elif (elif means else..if).

if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)

1
(3) Check whether a number is positive, negative, or zero.
number = int(input("Enter a number: ")
if number > 0:
print("Number is positive")
elif number < 0:
print("Number is negative")
else:
print("Number is zero")

(4) Display the appropriate message as per the colour of signal at the road
crossing.
signal = input("Enter the colour: ")
if (signal == "red" or signal == "RED"):
print("STOP")
elif (signal == "orange" or signal =="ORANGE"):
print("Be Slow")
elif (signal == "green" or signal == "GREEN"):
print("Go!")

(5) Write a program to create a simple calculator performing only four basic
operations.
val1 = float(input("Enter value 1: "))
val2 = float(input("Enter value 2: "))
op = input("Enter any one of the operator (+,-,*,/): ")
if op == "+":
result = val1 + val2
elif op == "-":
if val1 > val2:
result = val1 - val2
else:
result = val2 - val1
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Error! Division by zero is not allowed. Program
terminated")

2
else:
result = val1/val2
else:
print("Wrong input,program terminated")
print("The result is ",result)

INDENTATION
Python uses indentation for block as well as for nested block structures. Leading
whitespace (spaces and tabs) at the beginning of a statement is called indentation.
The interpreter checks indentation levels very strictly and throws up syntax errors if
indentation is not correct.

(6) Program to find the larger of the two pre-specified numbers


num1 = 5
num2 = 6
if (num1 > num2):
print("first number is larger")
else:
print("second number is larger")

REPETITION : A task which can be repeated within a limit time of interval is known
as repetition. In our syllabus there are two types of repetition process is knows as loop.
There are two types of loop .
(a) For loop (b) while loop

(a) For loop : The for statement is used to iterate over a range of values or a sequence.
The for loop is executed for each of the items in the range.

(7) Program to print even numbers in a given sequence using for loop
numbers = [1,2,3,4,5,6,7,8,9,10]
for num in numbers:
if (num % 2) == 0:
print(num,'is an even Number')

(8) Write a code to print the number from 1 to 10 using while loop
For i in range(1,11):
Print(i)

3
While loop :

Print the number from 1 to 12 using while loop


i=1
While(i<=12):
Print(i)
i=i+1

write a code to print the factorial of the given number :


no = 4
fact = 1
i=1
while(no>=i):
fact *= no*i
no = no -1

print(fact)

You might also like