Write a complete program in a single class called SumMaker w

Write a complete program in a single class called SumMaker, which reads in two integer values from the keyboard, say lowNum and highNum, and then prints the sum of all of the numbers from lowNum to highNum(inclusive). So if lowNum = 2 and highNum = 5, the program should print 14 = 2+3+4+5. If lowNum = 6, highNum = 6, the program should print 6. Also: if lowNum > highNum, the program should print 0.

Solution

SumMaker.java

import java.util.Scanner;


public class SumMaker {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter the lower number: \");
       int lowNum = scan.nextInt();
       System.out.print(\"Enter the higher number: \");
       int highNum = scan.nextInt();
       int sum = 0;
       for(int i=lowNum; i<=highNum; i++){
           sum = sum +i;
       }
       System.out.println(\"Sum is \"+sum);
   }

}

Output:

Enter the lower number: 2
Enter the higher number: 5
Sum is 14

Write a complete program in a single class called SumMaker, which reads in two integer values from the keyboard, say lowNum and highNum, and then prints the sum

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site