0% found this document useful (0 votes)
3 views

loops,break,continue

The document contains multiple Python programs demonstrating various functionalities, including calculating the sum of numbers in a list, generating Fibonacci numbers, printing prime numbers within a range, summing odd digits of a number, and identifying Armstrong numbers. It also explains the use of 'break' and 'continue' statements in loops. Each section includes example code and expected output.

Uploaded by

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

loops,break,continue

The document contains multiple Python programs demonstrating various functionalities, including calculating the sum of numbers in a list, generating Fibonacci numbers, printing prime numbers within a range, summing odd digits of a number, and identifying Armstrong numbers. It also explains the use of 'break' and 'continue' statements in loops. Each section includes example code and expected output.

Uploaded by

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

# Program to find the sum of all numbers stored in a list

numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11] # List of numbers


sum = 0 # variable to store the sum
for val in numbers: # iterate over the list
sum = sum+val
print("The sum is", sum)

Output:
The sum is 48
# This program computes the nth Fibonacci number

n = int(input("Enter value of n: "))


cur,prev = 1,1
print(cur,"\n",prev)
for i in range(n-2):
cur,prev = prev+cur,cur
Output:
print(cur)
Enter value of n: 3
print("The nth Fibonacci number is",cur)
1
1
2
The nth Fibonacci
number is 2
Print all the Prime Numbers within a Given Range

r=int(input("Enter upper limit: "))


for a in range(2,r+1):
k=0
for i in range(2,a//2+1):
if(a%i==0): Output:
k=k+1 Enter upper limit: 15
if(k<=0): 2
print(a) 3
5
7
11
13
Find sum of odd digits in a number

a=int(input("Enter number: "))


sum=0
while(a!=0):
d=a%10
if(d%2!=0):
sum=sum+d Output:
a=a//10 Enter number: 111
print("sum of odd digits=",sum) sum of odd digits= 3
Find Armstrong Number in an Interval using Python

for num in range(100,1000):


temp=num
sum=0
while temp>0: Output:
digit=temp%10 153
sum=sum+digit**3 370
371
temp=temp//10 407
if sum==num:
print (num)
Break statement
• The break statement terminates the
loop containing it.

• Control of the program flows to the


statement immediately after the body
of the loop.

• If break statement is inside a nested


loop (loop inside another loop),
break will terminate the innermost
loop.
Explanation
Example
• # Use of break statement inside loop

for val in "string":


if val == "i":
break
print(val)
print("The end")
Continue statement

• The continue statement is used


to skip execution of current
iteration.

• Loop does not terminate but


continues on with the next
iteration.
Explanation
Example
Thanks

You might also like