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.
( 10 pts.)

Solution

/**
* The java program SumMaker that prompts user to enter
* low number and high number and prints 0 if low is greater
* than high number and set total sum if low and high
* and total sum of low number and high number.
* */
//SumMaker.java
import java.util.Scanner;
public class SumMaker {
  
   public static void main(String[] args) {
      
       //declare variables of type integer
       int lowNum;
       int highNum;
       int totalSum=0;
       //Create a instance of Scanner classs
       Scanner scanner=new Scanner(System.in);
      
       System.out.println(\"Enter lowNum : \");
       //read lowNum
       lowNum=Integer.parseInt(scanner.nextLine());
              
       System.out.println(\"Enter lowNum : \");
       //read highNum
       highNum=Integer.parseInt(scanner.nextLine());
      
       //Check if low >high
       if(lowNum>highNum)
           totalSum=0;
       else if(lowNum==highNum)
           totalSum=lowNum;
       else
       {
           //sum of low and high inclusive
           for (int i = lowNum; i <=highNum; i++)
           {
               //add i value to totalSum
               totalSum+=i;
           }
       }
       //print total sum
       System.out.println(\"Total sum : \"+totalSum);
      
   }
  

}

-------------------------------------------------------

Sample Output:

Sample run1:
Enter lowNum :
10
Enter lowNum :
5
Total sum : 0

Sample run2:
Enter lowNum :
6
Enter lowNum :
6
Total sum : 6

Sample run3:
Enter lowNum :
2
Enter lowNum :
5
Total sum : 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
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