0% found this document useful (0 votes)
26 views6 pages

Practical No 5 Pytho

The document contains various Python programming exercises demonstrating the use of loops, functions, and basic algorithms. It includes examples of while loops, for loops, nested loops, and functions for generating patterns, calculating Fibonacci series, factorials, reversing numbers, summing digits, and checking for palindromes. Each section provides code snippets along with expected outputs.
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)
26 views6 pages

Practical No 5 Pytho

The document contains various Python programming exercises demonstrating the use of loops, functions, and basic algorithms. It includes examples of while loops, for loops, nested loops, and functions for generating patterns, calculating Fibonacci series, factorials, reversing numbers, summing digits, and checking for palindromes. Each section provides code snippets along with expected outputs.
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/ 6

Practical No:-5

Name:-Shweta Chavan Patil Roll No:-3207

1.While loop
num=int(input("Enter value="))
i=1
while i<=10:
print(num*i)
i=i+1
Output:-

2.For loop
num=int(input("Enter value="))
for i in range(1,11):
print(num*i)
i=i+1

Output:-

3.Nested for
for num in range(2,5):
for i in range(1,11):
print(num*i)
i=i+1
num=num+1
print(" ")
Output:-

Exercise:-
1.
n=4
for i in range(1, n+1):
print('*' * i)
Output:-

2.
def print_pattern(n):
# First half of the pattern (pyramid shape)
for i in range(1, n + 1):
# Print leading spaces
for j in range(1, n - i + 1):
print(" ", end="")
# Print stars
for j in range(1, 2 * i):
print("*", end="")
print()

# Second half of the pattern (inverted pyramid)


for i in range(n - 1, 0, -1):
# Print leading spaces
for j in range(1, n - i + 1):
print(" ", end="")
# Print stars
for j in range(1, 2 * i):
print("*", end="")
print()

n=3
print_pattern(n)
Output:-

3.
for i in range(7, 0, -2):
pattern = ''.join(['1' if j % 2 == 0 else '0' for j
in range(i)])
print(pattern)
Output:-
2.
def print_even_numbers():
num = 2 # Start from the first even number
while num<= 100:
print(num, end=' ')
num += 2 # Increment by 2 to get the next even number
print_even_numbers()
Output:-

3.
sum_natural = 0
for i in range(1, 11):
sum_natural += i # Add each number to sum
print("The sum of the first 10 natural numbers is:", sum_natural)
Output:-

4.
def fibonacci_series(n):
a, b = 0, 1 # First two terms
for _ in range(n):
print(a, end=" ") # Print current term
a, b = b, a + b # Update values
n_terms = int(input("Enter the number of terms: "))
print("Fibonacci Series:")
fibonacci_series(n_terms)
Output:-
5.
def factorial(n):
fact = 1
for i in range(1, n + 1):
fact *= i # Multiply each number
return fact
num = int(input("Enter a number: "))
if num< 0:
print("Factorial is not defined for negative numbers.")
else:
print(f"The factorial of {num} is: {factorial(num)}")
Output:-

6.
def reverse_number(n):
reversed_num = 0
while n > 0:
digit = n % 10 # Get the last digit
reversed_num = reversed_num * 10 + digit # Append digit to reversed_num
n //= 10 # Remove the last digit
return reversed_num
num = int(input("Enter a number: "))
print(f"The reversed number is: {reverse_number(num)}")
Output:-

7.
def sum_of_digits(n):
total = 0
while n > 0:
total += n % 10 # Extract last digit and add to total
n //= 10 # Remove last digit
return total
num = int(input("Enter a number: "))
print(f"The sum of digits in {num} is: {sum_of_digits(abs(num))}")
Output:-
8.
def is_palindrome(n):
return str(n) == str(n)[::-1] # Convert to string and compare with its reverse
num = int(input("Enter a number: "))
if is_palindrome(num):
print(f"{num} is a palindrome.")
else:
print(f"{num} is not a palindrome.")
Output:-

You might also like