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

Solution Set A, B

The document contains solutions to programming problems in Python, including counting even and odd numbers in a list, checking if a number is a palindrome, counting vowels and consonants in a string, and calculating the factorial of a number. Each solution includes code snippets, sample inputs, and expected outputs. The solutions demonstrate basic programming concepts and functions in Python.

Uploaded by

Artify It
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)
3 views6 pages

Solution Set A, B

The document contains solutions to programming problems in Python, including counting even and odd numbers in a list, checking if a number is a palindrome, counting vowels and consonants in a string, and calculating the factorial of a number. Each solution includes code snippets, sample inputs, and expected outputs. The solutions demonstrate basic programming concepts and functions in Python.

Uploaded by

Artify It
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

SOLUTION OF SET A

Q.01 Write a Python program to input a list of numbers and count how many numbers
are even and odd in the list. Display the count of even and odd numbers.

Sol. # Input list of numbers


numbers = list(map(int, input("Enter numbers separated by space: ").split()))

even_count = 0
odd_count = 0

# Checking each number


for num in numbers:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
# Display result
print("Number of even numbers:", even_count)
print("Number of odd numbers:", odd_count)

OUTPUT
//SAMPLE INPUT
Enter numbers separated by space: 10 15 20 25 30
//SAMPLE OUTPUT
Number of even numbers: 3
Number of odd numbers: 2
Q.02 Write a Python program using a function to check whether a given number is a
Palindrome or not. If the number is palindrome, display "Palindrome Number",
otherwise display "Not a Palindrome"

Sol. # Function to check palindrome


def check_palindrome(num):

original = num

reverse = 0

while num > 0:

digit = num % 10

reverse = reverse * 10 + digit

num = num // 10

if original == reverse:

print("Palindrome Number")

else:

print("Not a Palindrome")

# Input from user

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

check_palindrome(number)

OUTPUT

Sample Input
Enter a number: 121 , Enter a number: 123

Sample Output
Palindrome Number. , Not a Palindrome
Q.03 Consider the following table:

SOL.
SOLUTION OF SET B
1. Write a Python program to input a string and count the number of vowels and
consonants present in the string. Display the count of vowels and consonants
separately..
Sol. # Input string
s = input("Enter a string: ")

vowels = 0
consonants = 0

# Convert string to lowercase


s = [Link]()

# Check each character


for ch in s:
if [Link]():
if ch in 'aeiou':
vowels += 1
else:
consonants += 1

# Display result
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)
OUTPUT
Sample Input : Enter a string: Computer Science
Sample Output: Number of vowels: 6
Number of consonants: 9
Q.02 Write a Python program using a function to calculate the factorial of a given number. Display
the factorial.

SOL. # Function to calculate factorial


def factorial(n):
fact = 1
for i in range(1, n + 1):
fact = fact * i
return fact

# Input from user


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

# Function call and display result


print("Factorial of", num, "is:", factorial(num))

OUTPUT
Sample Input : Enter a number: 5
Sample Output : Factorial of 5 is: 120
Q.03

SOL.

You might also like