FindSum.java

less than 1 minute read

This program was created to find the sum of an interval of numbers by user input using the Scanner from Java and prints it out.

The code has been modified slightly and now uploaded to the site.

<— Return Home

Code Snippet

/*
Author: ENTPRESTIGIOUS
*/
import java.util.Scanner;
public class FindSum {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in); 
        final int INTERVALS = 3; 
        int start, end, sum; 
        for(int i=0; i<INTERVALS ; i++){ 
            System.out.print("Enter start and end of range: "); 
            start = input.nextInt(); 
            end = input.nextInt(); 
            sum = findSum(start, end); //calling function 
            System.out.printf("Sum from %d to %d is %d.\n\n", start, end, sum);
        } //end for }
    }
    // defining function 
    public static int findSum(int s, int e){ 
        int result = 0; 
        for (int i=s; i<=e; ++i){ 
            result += i;
        } //end for 
        return result; //return value
    }
}

<— Return Home