0% found this document useful (0 votes)
99 views3 pages

Python Looping Questions and Solutions

The document contains a series of programming questions and answers focused on loops in Python. It discusses suitable loop types for specific tasks, the output of a given code snippet, and the differences between break and continue statements. Additionally, it includes Python code examples for calculating factorials, counting positive/negative numbers, generating Fibonacci series, checking for prime numbers, and summing even numbers.

Uploaded by

mynamegurjot
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)
99 views3 pages

Python Looping Questions and Solutions

The document contains a series of programming questions and answers focused on loops in Python. It discusses suitable loop types for specific tasks, the output of a given code snippet, and the differences between break and continue statements. Additionally, it includes Python code examples for calculating factorials, counting positive/negative numbers, generating Fibonacci series, checking for prime numbers, and summing even numbers.

Uploaded by

mynamegurjot
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

Looping questions

1. A programmer needs to display each character of a string entered by the user one by one.
(a) Which loop is most suitable for this task?
(b) Explain the reason for your choice.
2. A student writes a program that keeps asking the user to enter a number until a negative number is
entered.
Identify the loop to be used and explain how the loop condition will work.
3. Observe the following code:
for i in range(2, 11, 2):
print(i)
(a) What will be the output of the code?
(b) State the role of each argument used in the range( ) function.
4. A loop in a program runs endlessly and the system becomes unresponsive.
(a) What is this situation called?
(b) State one logical error that may cause this problem.
5. Differentiate between break and continue statements using a suitable programming situation for each.
6. Explain the usefulness of nested loops with the help of a real-life programming example.
7. Write a Python program to display the factorial of a number entered by the user using a loop.
8. Write a Python program that takes 10 numbers from the user and counts how many of them are
positive, negative, and zero using a loop.
9. Write a Python program to generate the Fibonacci series up to n terms using a loop.
(Fibonacci series: 0,1,1,2,3,5,8,……..)
[Link] a Python program to check whether a number entered by the user is a prime number using a
loop.
[Link] a Python program to calculate the sum of all even numbers between 1 and 100 using a loop

Answers:
1. Display each character of a string one by one

(a) The most suitable loop is the for loop.

(b) A for loop is ideal because it can directly iterate over each character of a string without using an
index. This makes the code simple and readable.
2. Program keeps asking input until a negative number is entered

The while loop should be used.

Explanation:
The loop condition checks whether the entered number is non-negative. As long as the condition is
true, the loop continues. When a negative number is entered, the condition becomes false and the loop
stops.
3. Given code
for i inrange(2, 11, 2):
print(i)

(a) Output:
2
4
6
8
10

(b) Role of arguments in range( ):


2 → Starting value
11 → Ending value (not included)
2 → Step value (increment by 2)
4. Loop runs endlessly

(a) This situation is called an infinite loop.

(b) One logical error that causes this problem is incorrect loop condition (for example, the condition
never becomes false).
5. Difference between break and continue
break continue

Terminates the loop completely Skips the current iteration

Control comes out of the loop Control goes to the next iteration

Example situations:
break: Stop searching when an item is found
continue: Skip invalid input and continue checking others
6. Usefulness of nested loops
Nested loops are useful when a program needs to handle multiple levels of repetition.
Example:
Displaying a multiplication table where one loop controls rows and the inner loop controls columns.
7. Factorial of a number
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact = fact * i
print("Factorial =", fact)
8. Count positive, negative, and zero numbers
p=0
n=0
z=0
for i in range(5):
x = int(input("Enter a number: "))
if x > 0:
p=p+1
if x < 0:
n=n+1
if x = = 0:
z=z+1
print("Positive:", p)
print("Negative:", n)
print("Zero:", z)
9. Fibonacci series up to n terms
n = int(input("Enter number of terms: "))
a, b = 0, 1
for i in range(n):
print(a, end="")
a, b = b, a + b
10. Check whether a number is prime
n = int(input("Enter a number: "))
count = 0
for i in range(1, n + 1):
if n % i == 0:
count += 1
if count = = 2:
print("Prime number")
else:
print("Not a prime number")
11. Sum of all even numbers between 1 and 100
even = 0
for i in range(2, 101, 2):
even + = i
print("Sum of even numbers =", even)

You might also like