URI Online Judge Solution 1101 Sequence of Numbers and Sum using C Programming Language.
Read an 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).
Read an 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 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 Sample | Output Sample |
5 2
6 3 5 0 |
2 3 4 5 Sum=14
3 4 5 6 Sum=18 |
Solution using C :
#include <stdio.h>
int main() {
while(1)
{
int M,N,i,sum=0;
scanf("%d%d",&M,&N);
if(M<= 0 || N <= 0)
break;
if(M>N)
{
int t = M;
M = N;
N = t;
}
for(i=M; i<=N; i++)
{
printf("%d ",i);
sum += i;
}
printf("Sum=%d\n",sum);
}
return 0;
}
Comments
Post a Comment