0% found this document useful (0 votes)
1K views4 pages

PWP Practical No. 5

The document provides examples of using various looping statements like while, for, and nested loops in Python. It includes programs to print patterns using loops, print even numbers from 1 to 100 using while loop, find the sum of first 10 natural numbers using for loop, print Fibonacci series, calculate factorial of a number, reverse a given number, find sum of digits in a number, and check if a number is a palindrome.

Uploaded by

Rutuja Bhagat
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)
1K views4 pages

PWP Practical No. 5

The document provides examples of using various looping statements like while, for, and nested loops in Python. It includes programs to print patterns using loops, print even numbers from 1 to 100 using while loop, find the sum of first 10 natural numbers using for loop, print Fibonacci series, calculate factorial of a number, reverse a given number, find sum of digits in a number, and check if a number is a palindrome.

Uploaded by

Rutuja Bhagat
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

Practical No.

5: Write Python program to demonstrate use of looping


Statements: ‘while’ loop, ‘for’ loop and Nested loop.

1. Print the following patterns using loop:


a.
*
**
***
****

n = int(input("Enter the number of rows"))


for i in range(0, n):
for j in range(0, i + 1):
print("* ", end="")
print()

b.
*
***
*****
***
*

rows = int(input("Enter the number of rows: "))


k = 2 * rows - 2
for i in range(0, rows):
for j in range(0, k):
print(end=" ")
k=k-1
for j in range(0, i + 1):
print("* ", end="")
print("")

k = rows - 2
for i in range(rows, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k=k+1
for j in range(0, i + 1):
print("* ", end="")
print("")
c.
1010101
10101
101
1

num=int(input("Enter Number: "))


k=num-2
for i in range(num,0,-1):
a=(i*2)-1
for j in range(k, 0,-1):
print(end=" ")
k = k +1
for j in range(0,a,1) :
if j %2==0:
print(1,end="")
else:
print(0,end="")
print()

2. Write a Python program to print all even numbers between 1 to 100 using
while loop.
i=0
while i < 100:
i += 2
print("Even Number is :",i)

3. Write a Python program to find the sum of first 10 natural numbers using for
loop.
number = int(input("Please Enter any Number: \n "))
total = 0

for value in range(1, number + 1):


total = total + value

print("The Sum of Natural Numbers from 1 to {0} = {1}".format(number, total))

4. Write a Python program to print Fibonacci series.


nterms = int(input("How many terms you want? "))
n1 = 0
n2 = 1
count = 2
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence:")
print(n1)
else:
print("Fibonacci sequence:")
print(n1,",",n2,end=', ')
while count < nterms:
nth = n1 + n2
print(nth,end=' , ')
# update values
n1 = n2
n2 = nth
count += 1

5. Write a Python program to calculate factorial of a number


num = int(input("Enter a number: \n"))
factorial = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

6. Write a Python Program to Reverse a Given Number


number = int(input("Enter the integer number: \n"))
revs_number = 0
while (number > 0):
# Logic
remainder = number % 10
revs_number = (revs_number * 10) + remainder
number = number // 10

print("The reverse number is : {}".format(revs_number))


7. Write a Python program takes in a number and finds the sum of digits in a
number.
n=int(input("Enter a number:\n"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:",tot)

8. Write a Python program that takes a number and checks whether it is a


palindrome or not.
n=int(input("Enter number:\n"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")

You might also like