1. From the list below, which term/topic has NOT been discussed in lecture?
(type in the answer area below the one topic from the list of five that has not been discussed)
List:
looping
debugging
paging tables
precision
compiler
2. Write a while loop that accepts integers from the user until a negative number is entered. The prompt should be "Enter a number (negative to quit):" Add up the numbers and print the sum (not including the negative number). Assume and use the following declarations:
int sum = 0; int num;
Greetings!!
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sum=0;
int num;
while(1) //always enter into the loop
{
printf("Enter a number (negative to quit):\n"); //prompt message
scanf("%d",&num); //read the number from the user
if(num>0) //check whether the number is positive
sum=sum+num; //if the number is positive then find the sum
else
break; //go out of the loop if the number is negative
}
printf("Sum of the entered numbers: %d\n",sum); //print the sum
return 0;
}
Output screenshots:
Hope this helps
Thank You
Get Answers For Free
Most questions answered within 1 hours.