Write a program that reads numbers which are in the range 0 to 100, till it encounters -1. Print the sum of all the integers that you have read before you encountered -1
INPUT:
A sequence of integers separated by whitespace. There may be other integers following -1.
INPUT:
A sequence of integers separated by whitespace. There may be other integers following -1.
OUTPUT:
Sum of all integers in the sequence before you encounter -1. Any integer that is input after -1 in the sequence should be ignored.
CONSTRAINTS:
Atmost 10 integers will be given in the input. One of them is guaranteed to be a -1.
Inputs will be in the range 0 to 100 (both included).
Solution:
Sum of all integers in the sequence before you encounter -1. Any integer that is input after -1 in the sequence should be ignored.
CONSTRAINTS:
Atmost 10 integers will be given in the input. One of them is guaranteed to be a -1.
Inputs will be in the range 0 to 100 (both included).
Solution:
#include<stdio.h>
int main()
{
int sum=0;
int number=0;
do
{
sum=sum+number; //update partial sum
scanf("%d",&number); //read a number from the input
}while(number!=-1); //check whether recently added number is -1 or not
printf("%d",sum); //print the final sum
return 0;
}
int main()
{
int sum=0;
int number=0;
do
{
sum=sum+number; //update partial sum
scanf("%d",&number); //read a number from the input
}while(number!=-1); //check whether recently added number is -1 or not
printf("%d",sum); //print the final sum
return 0;
}
No comments:
Post a Comment