Write a C program, called integer.sum.c, that computes the sum of integers inter
ID: 3600825 • Letter: W
Question
Write a C program, called integer.sum.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 are not re- quired to introduce the constants 1, 2, and 3 into the program using #de fine, although you can do so. You can assume that when the user is prompted to enter an integer, the user indeed enters an integer. Here is one sample run of such a program, assuming that the executable file is called integer sum. Note that the input from the user is marked as underlined text below.Explanation / Answer
Here is your C program -
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=rand()%51;//0 to 50 any random number
int choice , g ,c=0, i=0, sum=0;
printf("Welcome to the integer sum program ");
while(1)
{
printf("So far you have entered %d numbers. You may ", c);
printf("1.Enter a new integer 2.Display the current sum 3.Exit Please enter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter a new integer ");
scanf("%d",&g);
sum=sum+g;
c++;
break;
case 2:
printf("Current sum is %d ",sum);
break;
case 3:
exit(0);
default :printf("Invalid choice ");
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.