C++ Programming Exercises and Solutions
C++ Programming Exercises and Solutions
The program uses a series of if-else if statements to determine and assign letter grades based on the calculated total of mid and final exam scores. The conditions check if the total falls within specific ranges (90-100 for 'A', 80-89 for 'B', etc.). This structure ensures clear, organized, and easily readable code where each range corresponds to a specific letter grade, minimizing error risk in the assignment .
The C++ program demonstrates sum computation using three loop constructs: for, while, and do-while. In the for loop, it initializes a counter and performs an iteration directly inside the loop definition. The while loop initializes the counter before the loop starts and checks the condition at the start of each iteration. The do-while loop also initializes before the loop, but unlike the while loop, checks the condition after each iteration, ensuring it runs at least once. These differences highlight the flexibility of each loop type in different contexts .
In the given C++ program, two nested loops are used: the outer loop controls the number of rows while the inner loop handles the printing of characters. The character 'alphabet' starts at 'a'. For each iteration of the outer loop from 1 to 6, the inner loop runs from 1 to the current outer-loop index. During each iteration of the inner loop, the current character in 'alphabet' is printed and incremented. This setup ensures an incremental, stacked pattern of characters based on ASCII values is printed across multiple lines .
The program uses a for loop to iterate from 1 to n, where n is the user-provided input. Within each iteration, it calculates the factorial of the current number 'i' using a cumulative product stored in 'fact'. It adds this 'fact' value to a running total 'sum'. The program outputs the total sum of factorial values after the loop concludes .
The program checks the validity of the input scores by using while loops that continue prompting the user to enter a correct range until it meets the condition. For the mid-term score, it ensures the user enters a value between 0 and 40, and for the final score, between 0 and 60. If the entered score is out of these ranges, the program displays a message requesting the user to enter a score within the specified range and exits with return(0) if an invalid input is detected .
The program initializes 'fact' to 1 and uses a for loop that iterates from the input number down to 1. Within the loop, it multiplies 'fact' by the iterator value 'i'. This effectively computes the factorial by multiplying all the integers from the input down to 1, storing the result in 'fact', which is then printed as the factorial result .
The program employs nested for loops where the outer loop iterates over values from 'A' to 'E', corresponding to ASCII values 65 to 69. The inner loop runs from 'A' up to the current value of the outer loop, printing characters. This approach effectively builds rows where each row contains an incrementally larger subset of the alphabet starting from 'A', creating the desired pyramid pattern of increasing character sequences .
The program iterates over numbers starting from 2 up to just below 100. For each number 'i', it initializes a flag 'prime' to 0, implying it starts by assuming 'i' is prime. A nested for loop checks divisors from 2 up to 'i/2'. If 'i' is divisible by any of these numbers (i.e., remainder is 0), the flag 'prime' is set to 1, indicating 'i' is not a prime, and the loop breaks. If 'prime' remains 0 after exiting the loop, 'i' is considered prime and is printed .