Need a program that contains a loop that asks the user to enter a series of posi
ID: 3642382 • Letter: N
Question
Need a program that contains a loop that asks the user to enter a series of positive numbers.The user should enter a negative number to signal the end of the series. After all the positive numbers have been entered, the program should display their sum.Explanation / Answer
Sum of Positive Input _________________ // i, sum, input are declared to be numbers (integers specifically using C programming code) and i will be used solely for looping conditions int i,sum,number; // sum is set to start at 0 for obvious reasons sum=0; // while input is greater than or equal to zero take the sum and add the input, if/when user enters -# then loop exits while(input>=0){ // ask user for input (in C "%d" tells the computer the type of input to expect, in this case an integer, and &input tells the computer to store the number entered in the "input" variable) scanf("%d",&input); // take the value for sum add the input and save it as the sum so we can add the next input from the user to sum on the next step of the loop sum=sum+number; } // while loop exits on entry of -# for input // time to display the sum (in C %d is the placeholder for a variable of type int, we tell it to display the value stored for the variable sum in this manner) printf("sum is %d",sum); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.