0% found this document useful (0 votes)
22 views

PWP 5

The document contains the code for 8 different Python programs/exercises including printing patterns using loops, finding even numbers using while loop, calculating sum of first 10 natural numbers using for loop, generating Fibonacci series, calculating factorial of a number, reversing a number, finding sum of digits in a number, and checking if a number is a palindrome.
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)
22 views

PWP 5

The document contains the code for 8 different Python programs/exercises including printing patterns using loops, finding even numbers using while loop, calculating sum of first 10 natural numbers using for loop, generating Fibonacci series, calculating factorial of a number, reversing a number, finding sum of digits in a number, and checking if a number is a palindrome.
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/ 9

PRACTICAL NO.

XI] EXERCISE

1) Print the following pattern using loop

**

***

****

 Program Code:

# Set the number of rows

num_rows = 4

# Outer loop for rows

for i in range(num_rows):

# Inner loop for columns

for j in range(i + 1):

print("*", end="")

print() # Move to the next line after each row

OUTPUT

b) *

***

*****

***

 Program Code:

# Set the number of rows for each half

num_rows_upper = 3

num_rows_lower = 2
# Upper half

for i in range(num_rows_upper):

# Print spaces before '*'

for space in range(num_rows_upper - i - 1):

print(" ", end="")

# Inner loop for columns

for j in range(2 * i + 1):

print("*", end="")

print() # Move to the next line after each row

# Lower half

for i in range(num_rows_lower):

# Print spaces before '*'

for space in range(i + 1):

print(" ", end="")

# Inner loop for columns

for j in range(2 * (num_rows_lower - i) - 1):

print("*", end="")

print() # Move to the next line after each row

OUTPUT
) 1010101

10101

101

Program Code:

# Set the number of rows

num_rows = 4

# Outer loop for rows

for i in range(num_rows):

# Print spaces before the first '1'

for space in range(i):

print(" ", end="")

# Inner loop for columns

for j in range(2 * (num_rows - i) - 1):

if j % 2 == 0:

print("1", end="")

else:

print("0", end="")

print() # Move to the next line after each row

OUTPUT
2) Write a python program to print all even numbers between 1 to 100 using while loop

 Program Code:

# Initialize a variable to start from 2

number = 2

# Use a while loop to iterate through even numbers

while number <= 100:

print(number, end=" ")

number += 2 # Increment by 2 to get the next even number

# Add a newline after printing all even numbers

print()

OUTPUT

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

 Program Code:

sum_of_numbers = 0

for number in range(1, 11):

sum_of_numbers += number

print(f"The sum of the first 10 natural numbers is: {sum_of_numbers}")

OUTPUT

3) Write a python program to print Fibonacci series


 Program Code:

# Function to generate Fibonacci series up to n terms

def fibonacci(n):

fib_series = [0, 1] # Initialize the series with the first two terms

for i in range(2, n):

next_term = fib_series[-1] + fib_series[-2]

fib_series.append(next_term)

return fib_series

num_terms = int(input("Enter the number of terms in the Fibonacci series: "))

if num_terms <= 0:

print("Please enter a positive integer.")

else:

# Generate and print the Fibonacci series

fib_result = fibonacci(num_terms)

print(f"Fibonacci series up to {num_terms} terms: {fib_result}")

OUTPUT

5) Write a Python Program to calculate the factorial of a number


Program Code:

# Function to calculate the factorial of a number

def factorial(n):

if n == 0 or n == 1:

return 1

else:

return n * factorial(n - 1)

# Get user input for the number

num = int(input("Enter a number to calculate its factorial: "))

# Check if the input is valid

if num < 0:

print("Factorial is undefined for negative numbers.")

else:

# Calculate and print the factorial

result = factorial(num)

print(f"The factorial of {num} is: {result}")

OUTPUT

6) Write a Python Program to Reverse a given number


Program Code:

# Function to reverse a number

def reverse_number(number):

reversed_num = 0

while number > 0:

digit = number % 10

reversed_num = (reversed_num * 10) + digit

number = number // 10

return reversed_num

# Get user input for the number

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

# Reverse the number and print the result

reversed_result = reverse_number(num)

print(f"The reversed number is: {reversed_result}")

OUTPUT

7) Write a Python Program takes in a number and finds the sum of digits in a number
 Program Code:

def sum_of_digits(number):

sum_digits = 0

while number > 0:

digit = number % 10

sum_digits += digit

number = number // 10

return sum_digits

num = int(input("Enter a number to find the sum of its digits: "))

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
Program Code:

# Function to check if a number is a palindrome

def is_palindrome(number):

original_number = number

reversed_num = 0

while number > 0:

digit = number % 10

reversed_num = (reversed_num * 10) + digit

number = number // 10

return original_number == reversed_num

# Get user input for the number

num = int(input("Enter a number to check if it is a palindrome: "))

# Check and print if the number is a palindrome

if is_palindrome(num):

print(f"{num} is a palindrome.")

else:

print(f"{num} is not a palindrome.")

OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy