Search
 
SCRIPT & CODE EXAMPLE
 

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);
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java how to convert string to int 
Java :: logical operators in java 
Java :: java replace string case insensitive 
Java :: java string regexp replace 
Java :: android remove focus from an element 
Java :: get raondom from array java 
Java :: enum with numbers java 
Java :: check if a string is empty java 
Java :: last char in string java 
Java :: viewpager2 dependency 
Java :: start activity for result deprecate 
Java :: export java_home linux 
Java :: sudoku solver 
Java :: public static void main vs static public void main 
Java :: java stack methods 
Java :: java store hexadecimal value 
Java :: how to fill a 2d array in java 
Java :: javafx textarea size 
Java :: java long to double 
Java :: java insert at index 
Java :: set textView.android:drawableEnd programmatically 
Java :: java for loop increment by 3 
Java :: java printf 
Java :: insert string in string java 
Java :: method resolve file in java 
Java :: linkedhashmap in java 
Java :: java combobox get selected item 
Java :: convert string into time in java 
Java :: add Duration to date in Kotlin 
Java :: how to get internet speed in android programmatically 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =