Skip to main content
Write a program to print integers from 1 to 6Output:
1
2
3
4
5
6

for Loop

Example for Loop

Exercise for Loop

Write a program using for loop to print odd integers from 1 to n (inclusive).Sample input:
10
Sample output:
1 3 5 7 9

while Loop

Exercise while Loop

Write a program using while loop to print integers from n to 1.Sample input: 10Sample output: 10 9 8 7 6 5 4 3 2 1

do-while Loop

Example do-while Loop

Exercise do-while Loop

Write a program using do-while loop to display the multiplication table of numbers entered by the user.Sample input:
5
Sample output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50\

Nested Loops

Exercise Nested Loops

Make a program to display m × n rectangles formed with star characters (*). m is the length of the rectangle, and n is its width.Sample Input:
7 3
Sample Output:
* * * * * * *
* * * * * * *
* * * * * * *
Write a program to display a numeric triangle illustrated as follows.Sample Input:
5
Sample Output:
1
12
123
1234
12345
Write a program to display a numeric triangle illustrated as follows.Sample Input:
5
Sample Output:
       ~~~~~~~ 1
     ~~~~~ 12
   ~~~ 123
 ~ 1234
12345
Write a program to display a numeric triangle illustrated as follows.Sample Input:
5
Sample Output:
       ~~~~~~~ 1
     ~~~~~ 121
   ~~~ 12321
 ~ 1234321
123454321
Write a program to display a numeric triangle illustrated as follows.Sample Input:
n = 5
Sample Output:
       ~~~~~~~ 1
     ~~~~~ 121
   ~~~ 12321
 ~ 1234321
123454321
 ~ 1234321
   ~~~ 12321
     ~~~~~ 121
       ~~~~~~~ 1

break dan continue

Using break

Using continue

Exercise break dan continue

By using break or continue, write a program to determine whether the number entered by the user is a prime number or not.Sample Input:
5
Sample Output:
Prime

Tambahan Materi Nested Loops di kelas

Tulis sebuah code program untuk menghitung total dari angka yang diinputkan oleh user.Sample Input:
Masukkan angka: = 12345
Sample Output:
5 4 3 2 1
sum of digits: = 15
Tulis sebuah code program untuk menghitung total dari angka yang diinputkan oleh user.Sample Input:
Masukkan angka: = 12345
Sample Output:
5 4 3 2 1
sum of digits: = 15
5 1
sum of digits: = 6\
Sample Output:
1 1 2 3 5 8 13 12 7 10 8 9 17 17 16 15 13 10 5 6 11 …
Status Exercise ProgressSample Input:
Masukkan angka: = 123
Masukkan angka: = 456
Masukkan angka: = 451
Sample Output:
1 + 2 = 3 (memenuhi syarat)
4 + 5 = 6 (tidak memenuhi syarat)
4 + 1 = 5 (memenuhi syarat)