0% found this document useful (0 votes)
44 views12 pages

Experiment 5 Programs

The document contains Python programs to print patterns using asterisks, print even numbers from 1 to 100 using a while loop, find the sum of the first 10 natural numbers using a for loop, print the Fibonacci series, calculate the factorial of a number, reverse a given number, find the sum of digits in a number, and check if a number is a palindrome.

Uploaded by

sangram.co10723
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)
44 views12 pages

Experiment 5 Programs

The document contains Python programs to print patterns using asterisks, print even numbers from 1 to 100 using a while loop, find the sum of the first 10 natural numbers using a for loop, print the Fibonacci series, calculate the factorial of a number, reverse a given number, find the sum of digits in a number, and check if a number is a palindrome.

Uploaded by

sangram.co10723
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/ 12

Name: Sangram S Supalkar

Branch: TYCO-B
Roll-84

Experiment-5
Q.1
a] python prog to print
*
**
***
****
# Python program to print a pattern of asterisks

# Set the number of rows for the pattern


num_rows = 4

# Outer loop for each row


for i in range(1, num_rows + 1):
# Inner loop to print asterisks in each row
for j in range(1, i + 1):
print('*', end='')

# Move to the next line after printing each row


print()
Name: Sangram S Supalkar
Branch: TYCO-B
Roll-84

b] python prog to print


*
***
*****
***
*
# Python program to print a specific pattern of asterisks

# Set the number of rows for the pattern


num_rows = 5

# Upper part of the pattern


for i in range(1, num_rows + 1, 2):
print('*' * i)

# Lower part of the pattern


for i in range(num_rows - 2, 0, -2):
print('*' * i)
Name: Sangram S Supalkar
Branch: TYCO-B
Roll-84

c] python program for


1010101
10101
101
1
# Python program to print a specific pattern

# Set the number of rows for the pattern


num_rows = 4

# Outer loop for each row


for i in range(num_rows, 0, -1):
# Inner loop to print 1s and 0s in each row
for j in range(1, i * 2, 2):
print('1' if j % 4 == 1 else '0', end='')

# Move to the next line after printing each row


print()
Name: Sangram S Supalkar
Branch: TYCO-B
Roll-84

Q.2 Even num using while loop


# Python program to print even numbers from 1 to 100 using while loop, one below
another

# Initialize the counter


num = 1

# While loop to iterate through numbers from 1 to 100


while num <= 100:
# Check if the current number is even
if num % 2 == 0:
# Print the even number with a new line character
print(num)

# Increment the counter


num += 1
Output
2
4
6
8
10
12
14
16
18
20
Name: Sangram S Supalkar
Branch: TYCO-B
Roll-84

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
Name: Sangram S Supalkar
Branch: TYCO-B
Roll-84

74
76
78
80
82
84
86
88
90
92
94
96
98
100
Name: Sangram S Supalkar
Branch: TYCO-B
Roll-84

3] python prog to find sum of 1st 10 natural num using for loop
# Python program to find the sum of the first 10 natural numbers using for loop

# Initialize the sum variable


sum_of_numbers = 0

# For loop to iterate through the first 10 natural numbers


for i in range(1, 11):
# Add each number to the sum
sum_of_numbers += i

# Print the sum


print("Sum of the first 10 natural numbers:", sum_of_numbers)
Output
Sum of the first 10 natural numbers: 55
Name: Sangram S Supalkar
Branch: TYCO-B
Roll-84

4] Python program to print Fibonacci series


# Python program to print the Fibonacci series

# Function to generate the Fibonacci series up to n terms


def fibonacci(n):
fib_series = []
a, b = 0, 1

for _ in range(n):
fib_series.append(a)
a, b = b, a + b

return fib_series

# Set the number of terms in the Fibonacci series


num_terms = 10

# Print the Fibonacci series


print("Fibonacci series up to", num_terms, "terms:")
print(fibonacci(num_terms))
Output
Fibonacci series up to 10 terms:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Name: Sangram S Supalkar
Branch: TYCO-B
Roll-84

5] Python program to calculate factorial of number


# Python program to calculate the factorial of a number

# Function to calculate the factorial


def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)

# Input: Get the number from the user


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

# Check for non-negative input


if num < 0:
print("Factorial is not defined for negative numbers.")
else:
# Calculate and print the factorial
result = factorial(num)
print("Factorial of", num, "is:", result)
Output
Enter a number to calculate its factorial: 5
Factorial of 5 is: 120
Name: Sangram S Supalkar
Branch: TYCO-B
Roll-84

Q. 6 Write a Python program to Reverse a given number.


no = int(input("Enter a number : "))
rev = 0
while no > 0:
rem = no % 10
rev = (rev * 10) + rem
no = no // 10
print("Reverse Number : ", rev)
Output
Enter a number : 4546
Reverse Number : 6454
Name: Sangram S Supalkar
Branch: TYCO-B
Roll-84

Q. 7 Write a Python program that takes in a number and finds the sum of digits in
a number.
num = int(input("Enter a number : "))
strnum = str(num)
sum = 0
for i in strnum:
sum += int(i)
print("Sun of digits : ", sum)
Output
Enter a number : 4546
Sun of digits : 19
Name: Sangram S Supalkar
Branch: TYCO-B
Roll-84

Q. 8 Write a Python program that takes a number and checks whether it is


pallindrome or not.
num = int(input("Enter a number : "))
strnum = str(num)
rev = ""
for i in range(len(strnum)-1, -1, -1):
rev += strnum[i]
revnum = int(rev)
if num == revnum:
print(num, "is Pallndrome")
else:
print(num, "is not Pallndrome")
Output
Enter a number : 12321
12321 is Pallndrome

You might also like