0% found this document useful (0 votes)
27 views2 pages

Practical Programs 1-5

Uploaded by

muthuuuakash
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)
27 views2 pages

Practical Programs 1-5

Uploaded by

muthuuuakash
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

ACCEPT A NUMBER AND PRINT ITS FACTORIAL

Aim: To accept a given number and to print the factorial value of the given number

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

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

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

Result:
The program was executed and expected output is displayed.
MENU DRIVEN PROGRAM TO COUNT CHARACTERS IN A STRING

Aim: To accept the string and to write a menu driven coding to count the no. of lowercase,
uppercase, digits, alphabets, space and special character.

text = input("Enter a string: ")

lowercase = 0
uppercase = 0
digits = 0
alphabets = 0
spaces = 0
special = 0

for ch in text:
if ch.islower():
lowercase += 1
if ch.isupper():
uppercase += 1
if ch.isdigit():
digits += 1
if ch.isalpha():
alphabets += 1
if ch == " ":
spaces += 1
if not ch.isalnum() and ch != " ":
special += 1

print("Number of lowercase letters:", lowercase)


print("Number of uppercase letters:", uppercase)
print("Number of digits:", digits)
print("Number of alphabets:", alphabets)
print("Number of spaces:", spaces)
print("Number of special characters:", special)
DISPLAY SUM OF ODD AND EVEN NUMBERS IN A LIST

Aim: To display the sum of odd and even numbers received and stored in a list.

numbers = eval(input("Enter a list of numbers: "))

even_sum = 0
odd_sum = 0

for num in numbers:


if num % 2 == 0:
even_sum += num
else:
odd_sum += num

print("Sum of even numbers:", even_sum)


print("Sum of odd numbers:", odd_sum)
DISPLAY FREQUENCY OF ELEMENTS ENTERED BY THE USER

Aim: To display the frequencies of all elements entered by the user.

l = eval(input("Enter: "))
for i in range(len(l)):
repeat = False
for j in range(i):
if l[i] == l[j]:
repeat = True
break
if not repeat:
print(l[i], ":", l.count(l[i]))
STRING MANIPULATION

Aim: To display the strings that start with the letter 'S' from the given list.

words = eval(input("Enter a list of strings: "))

for word in words:

if word.startswith('S') or word.startswith('s'):

print(word)

You might also like