Using a loop create a program that will prompts the user for two numbers and then print out summation list of all the between the two given numbers, starting fron the small number to the big number. For negative numbers use brackets when printing the summation.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int one = in.nextInt();
int two = in.nextInt();
int result = Math.min(one, two);
System.out.print(result < 0 ? "(" + result + ")" : result);
for (int i = Math.min(one, two) + 1; i <= Math.max(one, two); i++) {
result += i;
System.out.print(" + " + (i < 0 ? "(" + i + ")" : i));
}
System.out.println(" = " + result);
}
}
Comments
Leave a comment