44 lines
877 B
Java
44 lines
877 B
Java
package VL04.Aufgabe02;
|
|
|
|
/**
|
|
* Vorlesung 4 / Aufgabe 2
|
|
*
|
|
* @author Sebastian Brosch
|
|
*/
|
|
public class Aufgabe02 {
|
|
public static void main(String[] args) {
|
|
final int START = 1;
|
|
final int END = 20;
|
|
int sum = 0;
|
|
|
|
// create sum using for loop.
|
|
for (int i = START; i <= END; i++) {
|
|
sum += i;
|
|
}
|
|
|
|
System.out.printf("\nSumme aller Zahlen zwischen %d und %d: %d (for)", START, END, sum);
|
|
|
|
// create sum using while loop.
|
|
int j = START;
|
|
sum = 0;
|
|
|
|
while (j <= END) {
|
|
sum += j;
|
|
j++;
|
|
}
|
|
|
|
System.out.printf("\nSumme aller Zahlen zwischen %d und %d: %d (while)", START, END, sum);
|
|
|
|
// create sum using do while loop.
|
|
int k = START;
|
|
sum = 0;
|
|
|
|
do {
|
|
sum += k;
|
|
k++;
|
|
} while (k <= END);
|
|
|
|
System.out.printf("\nSumme aller Zahlen zwischen %d und %d: %d (do while)", START, END, sum);
|
|
}
|
|
}
|