Sample Programs in java

Java Example Program to find the sum of first N Numbers

[java]
/**
*@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
}
}
}

[/java]

Table of Contents

Output:

sum:1540