Chapter 3
Answer the following questions:
1. What are the different types of control structures?
Ans:
a. Sequential statements: Here, the statements are executed one after the other in the
order they appear.
[Link] statements: These are used to make decisions in programs. Python
provides:
if statement
if-else statement
if-elif-else ladder
nested if
c. Iterative statements: Used for repetition of a block of code.
2. Explain the use of if..else control structure?
Ans: The if…else control structure in Python is used for decision-making. It allows your
program to choose different paths of execution depending on a condition (whether it is
True or False).
Syntax-
if condition:
# block of code if condition is True
else:
# block of code if condition is False
3. Explain the use of if..elif..else statement.
Ans: The if…elif…else control structure is used when you need to check multiple
conditions and execute only one block of code depending on which condition is True.
Syntax-
if condition1:
# block of code if condition1 is True
elif condition2:
# block of code if condition2 is True
elif condition3:
# block of code if condition3 is True
...
else:
# block of code if all above conditions are False
4. What are iterative statements?
Iterative statements are the statements that allow a block of code to be repeated multiple times until a
certain condition is met. In other words, they make loops possible. They are also called looping
statements.
5. What is the difference between a flowchart and an algorithm.?
Algorithm Flowchart
A step-by-step written procedure to A pictorial/diagrammatic representation of the steps
solve a problem. of an algorithm.
Written in simple language,
Uses symbols like rectangles, diamonds, arrows, etc.
pseudocode, or steps.
Focuses on logic and sequence of
Focuses on visual representation of the process.
steps.
Easier for programmers to read and Easier for non-programmers or beginners to
implement. understand quickly.
Uses predefined symbols (Oval = Start/End,
No symbols, only text. Rectangle = Process, Diamond = Decision, Arrow =
Flow).
# Program to check if a number is a Buzz number
# A number is a buzz number if it ends with 7 or is divisible by 7
# Accept input from user
num = int(input("Enter a number: "))
# Check conditions for Buzz number
if num % 7 == 0 or num % 10 == 7:
print(num, "is a Buzz number.")
else:
print(num, "is not a Buzz number.")
# Program to check if a number is a Pronic number
# A number is a pronic number if it’s a product of two consequtive integers
# Accept input from user
num = int(input("Enter a number: "))
is_pronic = False
# Check by multiplying consecutive numbers
for i in range(num + 1):
if i * (i + 1) == num:
is_pronic = True
break
# Display result
if is_pronic:
print(num, "is a Pronic number.")
else:
print(num, "is not a Pronic number.")
1.
m=int(input(“Enter a number”))
n=int(input(“Enter a number”))
s=0
for i in range(m,n):
s=s+i
print(“Sum of numbers is ”,s)
---------------
m=1
n=5
s=0
i =1, s = 0+1 = 1
i=2, s= 1+2=3
i=3, s=3+3=6
i=4, s=6+4
i=5, s=10+5=15
2.
n=int(input(“Enter the level upto which the series needs to be printed: ”))
a=0
b=1
print(a)
print(b)
for i in range(1,n+1):
c=a+b
print(c)
a=b
b=c