URI Online Judge | 1101
Sequence of Numbers and Sum
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
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 |
URI Online Judge Solution 1101 || Sequence of Numbers and Sum in JAVA
import
java.io.IOException;
import
java.util.Scanner;
public
class
Main {
public
static
void
main(String[] args)
throws
IOException {
int
X, Y;
Scanner input =
new
Scanner(System.in);
while
(((X = input.nextInt()) >
0
)&&((Y = input.nextInt()) >
0
)) {
int
sum =
0
;
if
(X > Y) {
for
(Y = Y; Y <= X; Y++) {
sum+=Y;
System.out.print(Y+
" "
);
}
System.out.print(
"Sum="
+sum+
"\n"
);
}
else
{
for
(X = X; X <= Y; X++) {
sum+=X;
System.out.print(X+
" "
);
}
System.out.print(
"Sum="
+sum+
"\n"
);
}
}
}
}
URI Online Judge Solution 1101 || Sequence of Numbers and Sum in C
#include<stdio.h>
int main(){
int X, Y;
while (((scanf("%d", &X)) > 0 )&&(((scanf("%d", &Y)) > 0 ))) {
int sum = 0;
if (X > Y) {
for (Y = Y; Y <= X; Y++) {
sum+=Y;
printf("%d ", Y);
}
printf("Sum=%d\n", sum);
}else {
for (X = X; X <= Y; X++) {
sum += X;
printf("%d ", X);
}
printf("Sum=%d\n", sum);
}
}
}
URI Main Question link
Download dropbox code
Some Description about the problem:
Using a simple for loop I've just solve the problem.Look in the line
for
(Y = Y; Y <= X; Y++) {
sum+=Y;
System.out.print(Y+
" "
);
}
Here from two input range I've calculated the summation.
Note :
It is hardly required that you have to give all of the format what user can give the input. That means look-
if else statement
No comments:
Write commentsTo know more about the problem, give us your valuable commment. We'll try to help you. Thanks