import java.text.NumberFormat; public class LoopExample { public static void main(String[] args) { // Compute 1 + 2 + 3 + ... + 99 + 100 // With a while loop System.out.println("With a while loop"); int i = 1; int total = 0; while (i <= 100) { total += i; i++; } System.out.println("total = " + total + ", i = " + i); // another way i = 0; total = 0; while (i < 100) { i++; total += i; } System.out.println("total = " + total + ", i = " + i); // With a do-while loop System.out.println("\n\nWith a do-while loop"); i = 0; total = 0; do { i ++; total += i; }while(i < 100); System.out.println("total = " + total + ", i = " + i); // With a for loop System.out.println("\n\nWith a for loop"); total = 0; for (i = 1; i <= 100; i ++) { total += i; } System.out.println("total = " + total + ", i = " + i); // how many years to double an initial amount of $1000 // on a 2% savings account double amount = 1000; double rate = 10; // 2% int year = 0; do { amount += rate / 100 * amount; year ++; }while(amount <= 2000); NumberFormat nf = NumberFormat.getCurrencyInstance(); System.out.println("It takes " + year + " years to get " + nf.format(amount)); } }