Answer to Question #255429 in Java | JSP | JSF for Mike

Question #255429

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.

Sample Run1

Enter two numbers: 1 10

Output1: Sum = 1+2+3+4+5+6+7+8+9+10 =55


Sample Run2

Enter 2 numbers:-33

Output2: Sum=(-3)+(-2)+(-1)+0+1+2+3=0


1
Expert's answer
2021-10-22T16:43:48-0400
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);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS