Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

JAVA Display Sum of n Natural Numbers

// Program to find the sum of natural numbers from 1 to 1000.

class Main {
  public static void main(String[] args) {
      
    int sum = 0;
    int n = 1000;

    // for loop
    for (int i = 1; i <= n; ++i) {
      // body inside for loop
      sum += i;     // sum = sum + i
    }
       
    System.out.println("Sum = " + sum);
  }
}
 
PREVIOUS NEXT
Tagged: #JAVA #Display #Sum #Natural #Numbers
ADD COMMENT
Topic
Name
5+3 =