[go: up one dir, main page]

0% found this document useful (0 votes)
9 views5 pages

5th Practical

The document contains a series of Python programming exercises focusing on loops, including while and for loops, to perform various tasks such as printing numbers, generating patterns, calculating sums, and checking for palindromes. Each exercise includes the original code, the modified code where applicable, and the expected output. The exercises cover topics like even number generation, Fibonacci series, factorial calculation, and digit sum computation.

Uploaded by

prasad wadkar
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)
9 views5 pages

5th Practical

The document contains a series of Python programming exercises focusing on loops, including while and for loops, to perform various tasks such as printing numbers, generating patterns, calculating sums, and checking for palindromes. Each exercise includes the original code, the modified code where applicable, and the expected output. The exercises cover topics like even number generation, Fibonacci series, factorial calculation, and digit sum computation.

Uploaded by

prasad wadkar
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/ 5

5TH PRACTICAL

X.

1. What would be the output of following code:


x=10
while x>5:
print (x),
x-=1

Output:
10
9
8
7
6

2. Change the following Python code from using a while loop to for loop.
x=1
while x<10:
print (x),
x+=1

Program:
x=1
for x in range(1,10):
print(x)

Output:
1
2
3
4
5
6
7
8
9
1.
a.
Program:
for i in range(1,5):
print("*"*i)

Output:

b.
Program:
rows=3
for i in range(1,rows+1):
print(" "*(rows-i)+"*"*(2*i-1))
for i in range(rows-1,0,-1):
print(" "*(rows-i)+"*"*(2*i-1))

Output:

c.
Program:
rows=4
for i in range(rows,0,-1):
print(" "*(rows-i)+"1010101"[:2*i-1])

Output:

2. WAP to print all even numbers from 1-100 using while loop.
Program:
i=1
while i<=100:
if(i%2==0):
print("",i,end=””)
i+=1

Output:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46
48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88
90 92 94 96 98 100

3. WAP to find out the sum of first 10 natural numbers using for loop.
Program:
sum=0
for i in range(1,10+1):
sum=sum+i
print(sum)

Output:
55

4. WAP to print the Fibonacci series.


Program:
iteration=int(input("Enter the limit of series: "))
n1=0
n2=1
for i in range(iteration):
print(n1,end=" ")
temp=n1+n2
n1=n2
n2=temp

Output:

5. WAP to calculate the Factorial of a number.


Program:
number=int(input("Enter the number: "))
fact=1
while number>0:
fact=fact*number
number-=1
print(fact)

Output:

6. WAP to reverse a given number.


Program:
number=int(input("Enter a number to be reversed: "))
rev=0
while number>0:
n1=number%10
rev=rev*10+n1
number=number//10 #HERE WE HAVE USED FLOOR DIV TO
EXTRACT THE LAST DIGIT OF A NUMBER
print(rev)

Output:

7. WAP which takes in a number and finds the sum of digits in a number.
Program:
number=int(input("Enter the number: "))
sum=0
while number>0:
n1=number%10
sum=sum+n1
number=number//10
print(sum)

Output:

8. WAP which takes a number and checks it whether it is palindrome or not.


Program:
number=int(input("Enter a number: "))
String=str(number)
if String==String[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

Output:

You might also like