Vorlesung 4 / Aufgabe 2

This commit is contained in:
Sebastian Brosch 2024-05-09 12:59:05 +02:00
parent 98e7893343
commit 7c8a682066
2 changed files with 43 additions and 38 deletions

View File

@ -1,38 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
final int START = 1;
final int ENDE = 20;
int summe = 0;
int n = 0;
System.out.println("Lösung mit for-Schleife:");
for(n = START; n <= ENDE; n++) {
summe += n;
System.out.println(summe);
}
System.out.println("Lösung mit while-Schleife:");
summe = 0;
n = START;
while(n <= 20) {
summe += n;
System.out.println(summe);
n++;
}
System.out.println("Lösung mit do-while-Schleife:");
summe = 0;
n = START;
do {
summe += n;
System.out.println(summe);
n++;
} while (n <= ENDE);
}
}

View File

@ -0,0 +1,43 @@
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);
}
}