12:20 am - Tuesday May 22, 2018

Java Example Program to find the sum of first N Numbers

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
/**
*@author:Candidjava.com
*@discription: sum of first N numbers
*/
public class Sum {
	public static void main(String[] args) {
		int n = 10;
		int sum = 0;
		n = n * (n + 1) / 2;// calculate the sum N number
		for (int i = 0; i <= n; i++) {
			sum = sum + i;// calculate sum of i
			System.out.println("sum:" + sum);// display the vslue of sum
		}
	}
}

Output:

sum:1540

Filed in: Sample Programs in java

Comments are closed.