Java Examples - Sum of Numbers

Java 实例Java Examples

The following example demonstrates using the do...while structure to find the sum of integer numbers from 0 to 100:

Main.java file

public class Main { public static void main(String[] args) { int limit=100; int sum=0; int i=1; do { sum=sum+i; i++; } while(i<=limit); System.out.println("sum="+sum); } }

The output from running the above code is:

sum=5050

Java 实例Java Examples

Other extensions