Write an algorithm to read a value A and a value N. Print the sum of N numbers from A (inclusive). While N is negative or ZERO, a new N (only N) must be read. All input values are in the same line.
Input
The input contains only integer values, can be positive or negative.
Output
The output contains only an integer value.
Input Sample | Output Sample |
3 2
|
7
|
3 -1 0 -2 2
|
7
|
Solution using C :
#include <stdio.h>
int main() {
int a,n,s=0;
scanf("%d%d",&a,&n);
while(n<=0)
scanf("%d",&n);
while(n)
{
s += a++;
n--;
}
printf("%d\n",s);
return 0;
}
Comments
Post a Comment