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

Program 21

The document contains several Python programs demonstrating basic programming concepts. It includes code for printing patterns, checking if a number is even or odd, calculating the sum of digits, and determining if a number is a palindrome. Each program is accompanied by its respective code and expected output.

Uploaded by

haryyjazz2004
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)
5 views5 pages

Program 21

The document contains several Python programs demonstrating basic programming concepts. It includes code for printing patterns, checking if a number is even or odd, calculating the sum of digits, and determining if a number is a palindrome. Each program is accompanied by its respective code and expected output.

Uploaded by

haryyjazz2004
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

2332988 BTAIML503-20

Program 21(a): Print Pattern 1: using a for loop and range function
Code:

# Pattern 1
for i in range(1, 6):
for j in range(i):
print(i, end=" ")
print()

Output:
2332988 BTAIML503-20

Program 21(b): Print Pattern 2: using a for loop and range function

Code:
# Pattern 2
for i in range(1, 6):
for j in range(i, 0, -1):
print(j, end=" ")
print()

Output:
2332988 BTAIML503-20

Program 22: To check whether a given number is even or odd

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

if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")

Output:
2332988 BTAIML503-20

Program 23: To find sum of all the digits of a given number

Code:
num = int(input("Enter a number: "))
total = 0

for digit in str(num):


total += int(digit)

print("Sum of digits:", total)

Output:
2332988 BTAIML503-20

Program 24: To check whether given number is palindrome or not

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

if num == num[::-1]:
print(num, "is a Palindrome")
else:
print(num, "is Not a Palindrome")

Output:

You might also like