Program to find sum of first n numbers in java

Let’s see program to find sum of first n numbers in java.

 

public class Example {
	
	public static int sumOfNumbers(int number) {
		int sum = 0;
		
		for(int i = 1;i <= number;i++) {
			sum = sum + i;
		}
		
		return sum;
	}
	
public static void main(String[] args) {
	
	System.out.println("The Sum Of first " +5+" Numbers is : " + sumOfNumbers(5));
}
}

Output is – The Sum Of first 5 Numbers is : 15