CS100 Pa5
CS100 Pa5
Submission Guidelines
• Combine all .cpp files into a folder, zip it, and rename as 2XXXXXXX PA5.zip.
• Submit your zipped file via the designated LMS portal before the deadline.
Important Notes
• This assignment is a practical exercise to enhance your learning; dishonest practices will
impede your progress.
• Print a right-angle triangle where each row contains the row number repeated accord-
ingly.
Sample Output
1
Problem 2: Sum of First and Last Digits [10]
Write a program in C++ to find the sum of the first and last digits of any integer. The
program should:
• Display the first digit, the last digit, and their sum.
Sample Output
Example
Input: 12345
Output: 9 (since 1 + 3 + 5 = 9)
Input: 24689
Output: 9
Input: 0
Output: (end program)
2
Problem 4: Power Calculation Without Built-In Functions [10]
Write a program in C++ to calculate and display the powers of a given number up to a
specified maximum exponent without using any built-in functions (such as pow). The program
should:
• Use loops to compute the power for each exponent from 0 up to the maximum exponent.
Sample Output
Example 1
Input:
Enter N: 10
Output:
2 3 5 7 11 13 17 19 23 29
Example 2
Input:
Enter N: 5
Output:
2 3 5 7 11
3
Hint A prime number is a number greater than 1 that has no divisors other than 1 and
itself. Use a nested loop to check divisibility and determine if a number is prime.
Write a program in C++ to determine the frequency of each digit (0 through 9) in a given
integer. The program should:
• Analyze the number to count how many times each digit appears.
• NOTE: You must do this using nested loops without declaring a separate count variable
for each digit.
Sample Output
Write a C++ program that performs basic string manipulation. The program should:
1. Use getline(cin, str) to read an entire line of text (since cin stops at spaces).
2. Use a loop to iterate over the string and print each character on a new line. Use
str.length() to get the length of the string.
4. Identify any spaces in the string and print the indices at which they are found.
4
• Reading Input: Use the following code to read a full line from the user:
string str;
getline(cin, str);
• Accessing Characters Using Indexing: To access and print each character of a string,
you can use a loop with indexing that iterates over the string’s length. str[0] gives
the first character in the string, str[1] gives the second character and so on and so
forth. If str is of length n, then the last character is at index n-1.
For example:
This loop iterates over the entire string and prints each character (including spaces).
• Identifying Spaces and Replacing Them: You can check if a character is a space by
comparing it to ’ ’. To replace a space with an asterisk, use:
if (str[i] == ’ ’)
{
str[i] = ’*’;
}
Sample Output:
Input a string: Hello World!
H
e
l
l
o
W
o
r
l
d
!
Spaces found at indices: 5
Modified string: Hello*World!
5
Part B: Print the String in Reverse
Based on what you learned in Part A, extend your program to print the entire (modified)
string in reverse order. Use a loop to access characters in reverse (starting from the last
index and moving to the first).