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

Programming Assignment Unit 5

The document provides a Python program that takes a user's name as input and performs three operations on it - displaying characters from the left, counting vowels, and reversing the name. The program uses functions to implement each operation and a main loop to get user input and call the appropriate function.

Uploaded by

Zafarullah Memon
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
507 views

Programming Assignment Unit 5

The document provides a Python program that takes a user's name as input and performs three operations on it - displaying characters from the left, counting vowels, and reversing the name. The program uses functions to implement each operation and a main loop to get user input and call the appropriate function.

Uploaded by

Zafarullah Memon
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming Assignment Unit 5

CS 1101-01 Programming Fundamentals - AY2024-T1


Submitted by: Zafarullah

Write program to display your name and perform following operations on it:
1. Display n characters from left. (Accept n as input from the user)
2. Count the number of vowels.
3. Reverse it.

Solution:

# Function to display n characters from the left


def display_left_characters(name, n):
left_chars = name[:n]
print(f"{n} characters from the left: {left_chars}")

# Function to count the number of vowels


def count_vowels(name):
vowels = "AEIOUaeiou"
vowel_count = sum(1 for char in name if char in vowels)
print(f"Number of vowels: {vowel_count}")

# Function to reverse the name


def reverse_name(name):
reversed_name = name[::-1]
print(f"Reversed name: {reversed_name}")

# Main program
your_name = input("Enter your name: ")

while True:
print("\nChoose an operation:")
print("1. Display n characters from left")
print("2. Count the number of vowels")
print("3. Reverse the name")
print("4. Exit")

choice = input("Enter your choice (1/2/3/4): ")

if choice == '1':
n = int(input("Enter the number of characters from the left: "))
display_left_characters(your_name, n)
elif choice == '2':
count_vowels(your_name)
elif choice == '3':
reverse_name(your_name)
elif choice == '4':
break
else:
print("Invalid choice. Please enter a valid option (1/2/3/4).")

print("Goodbye!")

You might also like