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

Pract5

Uploaded by

Ritesh Doibale
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)
28 views5 pages

Pract5

Uploaded by

Ritesh Doibale
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

Subject: PWP[22616]

Practical No. 5: Looping Statements : while loop,For loop and nested loop
Name: Ritesh Doibale Roll no:41
Practical code:

While loop:

Code:

count = 0
while (count < 5):
print('The count is ', count)
count = count + 1
print("Good bye!")
Output:

For loop:

for letter in 'Python':


print('Current Letter :', letter)
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # Second Example
print('Current fruit :', fruit)
print("Good bye!")
Output:
Subject: PWP[22616]
Practical No. 5: Looping Statements : while loop,For loop and nested loop
Name: Ritesh Doibale Roll no:41
Nested loop:

i = 2

while(i<20):
j = 2
while(j <= (i/j)):
if not(i%j): break
j = j + 1
if (j > i/j) : print(i, " is prime")
i = i + 1
print("Good bye!")
Output:

XI.Exercise.

1.-->

aCode:

for i in range(1, 5):


print('*' * i)

Output:
Subject: PWP[22616]
Practical No. 5: Looping Statements : while loop,For loop and nested loop
Name: Ritesh Doibale Roll no:41
bCode:

rows = 5
for i in range(1, rows, 2):
print(' ' * ((rows - i) // 2) + '*' * i)
for i in range(rows, 0, -2):
print(' ' * ((rows - i) // 2) + '*' * i)

Output:

2.-->Code:

# Python program to print even numbers between 1 and 100 using while loop
number = 1
while number <= 100:
if number % 2 == 0:
print(number)
number += 1

Output:
Subject: PWP[22616]
Practical No. 5: Looping Statements : while loop,For loop and nested loop
Name: Ritesh Doibale Roll no:41
3. Code:

sum_of_numbers = 0
for number in range(1, 11):
sum_of_numbers += number
print("Sum of the first 10 natural numbers:", sum_of_numbers)

Output:

4. Code:

def fibonacci_series(n):
fib_series = [0, 1]
while len(fib_series) < n:
next_number = fib_series[-1] + fib_series[-2]
fib_series.append(next_number)
return fib_series
num_terms = int(input("Enter the number of Fibonacci terms to generate: "))
result = fibonacci_series(num_terms)
print("Fibonacci series up to {} terms:".format(num_terms), result)

Output:

5. Code:

def calculate_factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * calculate_factorial(n - 1)
number = int(input("Enter a number to calculate its factorial: "))
if number < 0:
print("Factorial is not defined for negative numbers.")
else:
factorial_result = calculate_factorial(number)
print("Factorial of {} is: {}".format(number, factorial_result))
Output:
Subject: PWP[22616]
Practical No. 5: Looping Statements : while loop,For loop and nested loop
Name: Ritesh Doibale Roll no:41

6. Code:

def reverse_number(num):
reversed_num = 0
while num > 0:
digit = num % 10
reversed_num = (reversed_num * 10) + digit
num = num // 10

return reversed_num
number = int(input("Enter a number to reverse: "))
result = reverse_number(number)
print("Reversed number:", result)

Output:

7. Code:

def sum_of_digits(num):
digit_sum = 0
while num > 0:
digit = num % 10
digit_sum += digit
num = num // 10

return digit_sum
number = int(input("Enter a number to find the sum of its digits: "))
result = sum_of_digits(number)
print("Sum of digits in {} is: {}".format(number, result))
Output:

You might also like