Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Summation (Sum of Numbers)

Java Example – Summation (Sum of Numbers)

Java Examples Java Examples

The following example demonstrates using a do...while loop to compute the sum of integers 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 of the above code is:


sum=5050

Java Examples Java Examples