[URI Online Judge] – 1101 – Sequence of Numbers and Sum

Read a undetermined number of pairs values M and N (stop when any of these values is less or equal to zero). For each pair, print the sequence from the smallest to the biggest (including both) and the sum of consecutive integers between them (including both).

Input

The input file contains pairs of integer values M and N. The last line of the file contain a number zero or negative, or both.

Output

For each pair of values??, print the sequence from the smallest to the biggest and the sum of these values, as shown below.

Sample Input Sample Output
5 2
6 3
5 0
2 3 4 5 Sum=14
3 4 5 6 Sum=18

Solution:

#include <cstdio>
int main(){
 int i, j, x, z, s;
 while (scanf("%d %d", &i, &j) && i > 0 && j > 0) {
 s = 0;
 if (j < i){
 x = i; i = j; j = x;
 }
 for (z = i; z <= j; z++){
 printf("%d ", z);
 s += z;
 }
 printf("Sum=%d\n", s);
 }
 return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s