Write a method called SumRange that accepts two integer para

Write a method called SumRange that accepts two integer parameters that form a range of numbers. You may assume that the first parameter is the lower or lesser number and that the second parameter is a higher or greater number. The method should use a for loop to sum up all of the numbers between and including the numerical first and the second parameter values. This method should also return the summed value to its calling statement. For example: numbers 1 and 5 as the parameter values would sum up 1 + 2 + 3 + 4 + 5 to produce a returned value of 15.

Solution

package org.students;

import java.util.Scanner;

public class AddNumberInRange {

   public static void main(String[] args) {
       //Declaring variables
       int first,second,sum;
      
       //Scanner clss object is used to read the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Getting the number entered by the user
       System.out.print(\"Enter First number :\");
       first=sc.nextInt();
      
      
       /* This loop continue to execute until the user enters
       * second number which is greater than first number
       */
       while(true)
       {
           //Getting the second number entered by the user
           System.out.print(\"\ Enter Second number :\");
           second=sc.nextInt();
          
           /* checking whether the second number is less than first number or not.
           * If yes,Then display the error message and Prompt user again .
           * If not,then control comes out of the while loop
           */
           if(second<first)
           {
               //Displaying the error message
               System.out.println(\":: Invalid Number.Second Number Must be Greater than First Number ::\");
               continue;
           }
           else
               break;
       }

       //calling the method sumRange() by passing the first number and second number as arguments
   sum=sumRange(first,second);
     
   //Displaying the sum of all the numbers between the range
       System.out.println(\"The Sum of numbers between \"+first+\" and \"+second+\" is :\"+sum);

   }

   /* This method will calculate the sum of all the numbers
   * between first number and second number (inclusive)
   * Params :first number,second number of type integer
   * Return : sum of type integer
   */
   private static int sumRange(int first, int second) {
       //Declaring local variable
       int sum=0;
      
       //This loop will calculate the sum of all the numbers between the range
       for(int i=first;i<=second;i++)
       {
           //calculating the sum
           sum+=i;
       }  
       return sum;
   }

}

_______________________________

output:

Enter First number :1

Enter Second number :5
The Sum of numbers between 1 and 5 is :15

_______________Thank You

Write a method called SumRange that accepts two integer parameters that form a range of numbers. You may assume that the first parameter is the lower or lesser
Write a method called SumRange that accepts two integer parameters that form a range of numbers. You may assume that the first parameter is the lower or lesser

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site