[go: up one dir, main page]

0% found this document useful (0 votes)
77 views3 pages

CS 1101-01 Unit 5

Programming Fundamentals Unit 5 Assignment

Uploaded by

sarahhauya1
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)
77 views3 pages

CS 1101-01 Unit 5

Programming Fundamentals Unit 5 Assignment

Uploaded by

sarahhauya1
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/ 3

University of the People

Sarah Hauya

CS 1101-01: Programming Fundamentals

Unit 5 Programming Assignment

Muhammad Anwa, Instructor

Due by 10th October, 2024


Introduction

To preamble, this Programming assignment will answer the folliwing given problem;

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.

The code and its output must be explained technically. The explanation can be provided before or
after the code, or in the form of comments within the code. The descriptive part of your response
must be at least 200 words.

Code for first question;

my_name = input ("Enter your name: ")

n = int (input ("Enter the number of characters to display from left: "))

left = my_name[:n]print("Left characters:", left)

Explanation to the above;

The program will begin by asking the user to enter his/her name using the input() function and will keep
it in variable called my_name. The user will be instructed to enter the number of characters he like to
print from the left side by using the input() function and converts it into an integer by using the int()
function. This value will be stored in the variable n.For displaying the characters starting from Left, the
program will use slicing with my_name[:n] andit will return a substring which contains the first n
characters of the my_name. The result will bestored in the variable named left and will be printed.

Output from code:

Enter your name: Atu Alem

Enter the number of characters to display from left:

3Left characters: Atu


Code for second part:

vowels = ['a', 'e', 'i', 'o', 'u']

count = 0

count += 1

print("Number of vowels:", count)

Explanation to above;

In order to count the number of vowels, the program initialises a variable named count to 0. By using a
forloop, it will iterate upon every character in variable my_name which was used in first part of the
program. For instance, If the program finds the lowercase version of the character is found, count
variable is incremented by 1. Then the program will print the value of count.

Output from code:

Number of vowels: 3.

Code for the last part:

r_name = my_name [::-1]print ("Reverse name:", r_name)Output:

Reverse name: melA utA

Explanationto this part:

For this section of assignment, this program will use slicing with the variable my_name[::-1] syntax; this
will return a reversed model of it. The result will be stored in the r_name variable and it will be finally
printed.

Conclusion

Above are explanations and solutions to the given problems in this week 5 Programming assignment
part.

You might also like