1101 Sequence of Numbers and Sum

SemiColon
By -
0
URI Online Judge | 1101

Sequence of Numbers and Sum

Read an undetermined number of pairs values 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 and N. The last line of the file contains a number zero or negative, or both.

Output

For each pair of numbers, print the sequence from the smallest to the biggest and the sum of these values, as shown below.
Input SampleOutput Sample
5 2
6 3
5 0
2 3 4 5 Sum=14
3 4 5 6 Sum=18

 Solution:

#include <stdio.h>
int main()
{
    int a,b,c,d=0;
    while(1)
    {
        scanf("%d%d", &a, &b);
        if(a<=0 || b<=0)
            break;
        else
        {
            d=0;
            if(a<b)
            {
                for(c=a; c<=b; c++)
                {
                    printf("%d ",c);
                    d+=c;
                }
                printf("Sum=%d\n",d);
            }
            else if(a>b)
            {
                for(c=b; c<=a; c++)
                {
                    printf("%d ",c);
                    d+=c;
                }
                printf("Sum=%d\n",d);
            }
        }
    }
    return 0;
}

Tags:

Post a Comment

0Comments

Post a Comment (0)