Hi guys,

I want to find the sum of numbers(inclusive) between TWO integers inputted by user.

Example: if user enters 1 and 4, then it outputs: Sum = 10

I just started my code:

public static void main(String[] args) {
        int number1 = 0,number2 = 0,Sum = 0;
        Scanner input = new Scanner(System.in);
        System.out.print("first integer:");
        number1 = input.nextInt();
        System.out.print("second integer:");
        number2 = input.nextInt();

    }

How can I do it? Do I have to use loops?

Recommended Answers

You don't need to use loops.
You can

System.out.println(number1+number1+number2+number2);

unless you need to create a function.

private static int sumInclusive(int number1, int number2)
{
   return number1+number1+number2+number2;
}
Jump to Post

All 2 Replies

You don't need to use loops.
You can

System.out.println(number1+number1+number2+number2);

unless you need to create a function.

private static int sumInclusive(int number1, int number2)
{
   return number1+number1+number2+number2;
}

well known method:

(( 1stNumber + 2ndNumber ) * numberOfNumbers) / 2

eg. 1st Number = 4 2nd = number 8

( ( 4 + 8 ) * ( 8 - 4 + 1 ) ) / 2
( 12 * 5 ) / 2
= 30

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.