Write a C that computes the sum of integers interactively provided by the user.
ID: 3623157 • Letter: W
Question
Write a C that computes the sum of integers interactively provided by the user. At each iteration, the program should print the number of integers that the user has entered so far, along with a menu describing three possible actions from which the user could choose. The three possible actions are as follows:1. Entering a new integer
2. Printing the sum of all the integers entered so far
3. Exiting the program
If the user enters a number other than 1, 2, or 3 when presented with this menu, the program should simply print the same menu again, prompting the user to enter a new choice of action.You can assume that when the user is prompted to enter an integer, the user indeed enters an integer.
Here is a sample run:
So far, you have entered 0 numbers. You may:
1. Enter a new integer
2. Display the current sum
3. Exit
Please enter your choice: 2
The current sum is: 0
So far, you have entered 0 numbers. You may:
1. Enter a new integer
2. Display the current sum
3. Exit
Please enter your choice: 1
Enter the new integer: 30
So far, you have entered 1 numbers. You may:
1. Enter a new integer
2. Display the current sum
3. Exit
Please enter your choice: 1
Enter the new integer: -17
So far, you have entered 2 numbers. You may:
1. Enter a new integer
2. Display the current sum
3. Exit
Please enter your choice: 2
The current sum is: 13
So far, you have entered 2 numbers. You may:
1. Enter a new integer
2. Display the current sum
3. Exit
Please enter your choice: 6
So far, you have entered 2 numbers. You may:
1. Enter a new integer
2. Display the current sum
3. Exit
Please enter your choice: 2
The current sum is: 13
So far, you have entered 2 numbers. You may:
1. Enter a new integer
2. Display the current sum
3. Exit
Please enter your choice: 1
Enter the new integer: 42
So far, you have entered 3 numbers. You may:
1. Enter a new integer
2. Display the current sum
3. Exit
Please enter your choice: 3
Explanation / Answer
#include int main() { int number, sum, choice, total_numbers; printf("Welcome to the integer sum program!nn"); choice = 0; total_numbers = 0; sum = 0; while(choice!=3){ printf(" So far, you have entered %d numbers. You may:" " 1. Enter a new integer" " 2. Display the current sum" " 3. Exit" " Please enter your choice:", total_numbers); scanf("%d", &choice); switch(choice){ case 1: printf("Enter a new integer:"); scanf("%d", &number); sum+=number; total_numbers++; break; case 2: printf("The current sum is: %d ", sum); break; } }while(choice!=3); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.