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

Prac 5

The document contains a series of Python programming exercises aimed at practicing loops and basic programming concepts. It includes code snippets for printing patterns, calculating sums, generating Fibonacci series, computing factorials, reversing numbers, summing digits, and checking for palindromes. Each exercise is accompanied by the corresponding code and expected output.

Uploaded by

Sumedh Raut
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)
14 views5 pages

Prac 5

The document contains a series of Python programming exercises aimed at practicing loops and basic programming concepts. It includes code snippets for printing patterns, calculating sums, generating Fibonacci series, computing factorials, reversing numbers, summing digits, and checking for palindromes. Each exercise is accompanied by the corresponding code and expected output.

Uploaded by

Sumedh Raut
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/ 5

dd

NAME: SUMEDH SANJAY RAUT


CLASS: CO6I(A)
ROLL NO: 532
SUBJECT: PYTHON(22616)

PRACTICAL NO: 5
XI. Exercise
1.Print the following patterns using loop:
a)
Code:
n=4
for i in range(1, n + 1):
print('*' * i)
Output:

b)
Code:
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:
dd

c)
Code:
n=7
for i in range(0, n // 2 + 1):
for j in range(n - 2 * i):
if j % 2 == 0:
print('1', end='')
else:
print('0', end='')
print()
Output:

2. Write a Python program to print all even numbers between 1 to 100 using while loop.

Code:
num = 2
while num <= 100:
print(num, end=" ")
num += 2
Output:

3. Write a Python program to find the sum of first 10 natural numbers using for loop.

Code:
total = 0
for num in range(1, 11):
total += num

print("The sum of the first 10 natural numbers is:", total)


dd

Output:

4. Write a Python program to print Fibonacci series.

Code:

n = int(input("Enter the number of terms: "))

a, b = 0, 1

print("Fibonacci Series:", end=" ")

for _ in range(n):
print(a, end=" ")
a, b = b, a + b # Update to the next terms
Output:

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


Code:

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


factorial = 1

if num < 0:
print("Factorial is not defined for negative numbers.")
elif num == 0:
print("The factorial of 0 is 1.")
else:
dd

for i in range(1, num + 1):


factorial *= i
print(f"The factorial of {num} is {factorial}.")
Output:

6. Write a Python Program to Reverse a Given Number


Code:

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

reversed_num = 0

while num > 0:


digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10

print("The reversed number is: {reversed_num}")


Output:

7. Write a Python program takes in a number and finds the sum of digits in a number.
Code:

def sum_of_digits(number):
total = 0
while number > 0:
total += number % 10
number //= 10
return total
dd

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

result = sum_of_digits(num)
print(f"The sum of digits in {num} is {result}.")
Output:

8. Write a Python program that takes a number and checks whether it is a palindrome or
not.
Code:

def is_palindrome(number):
num_str = str(number)
return num_str == num_str[::-1]

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