pangfengliu / programmingtasks

programming tasks from my courses
67 stars 17 forks source link

Sum from i to j #11

Open pangfengliu opened 10 years ago

pangfengliu commented 10 years ago

Task Description

Write a program to compute the sum from i to j, where i < j, and both i and j are positive integers.

Input Format

There are two lines in the input. The first line has the integer i. The second line has the integer j.

Output Format

There are one line in the output. The line has the sum from i to j.

Sample Input

2 5

Sample Output

14

linsanity711 commented 6 years ago

include

int main(void) { // your code goes here int a, b; int number; int sum = 0; scanf("%d%d", &a, &b); number = a; while ( number <= b ) { sum += number; number += 1; } printf("%d", sum);

return 0;

}

Stacyaaa commented 1 week ago

include

main() { int i = 1; int sum = 0; int j; scanf("%d", &j); while (i <= j){ sum += i; i++; } printf("%d\n", sum); }

Stacyaaa commented 1 week ago

include

main() { int i, j, sum = 0; scanf("%d", &j); for (i = 1; i <= j; i++) sum += i; printf("%d", sum); return 0; }