1.
For Loop Examples
Example 1: Print numbers from 1 to 5 using a for loop
public class ForLoopExample1 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
[Link](i);
Output:
Example 2: Calculate the sum of the first 5 natural numbers
public class ForLoopExample2 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
[Link]("Sum: " + sum);
Output:
Sum: 15
Example 3: Print the elements of an array using a for loop
public class ForLoopExample3 {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < [Link]; i++) {
[Link](numbers[i]);
}
}
Output:
Exercises:
1. Write a program that prints the first 10 even numbers using a for loop.
2. Write a program to print the multiplication table of 5 using a for loop.
3. Write a program that finds the factorial of a number using a for loop.
2. While Loop Examples
Example 1: Print numbers from 1 to 5 using a while loop
public class WhileLoopExample1 {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
[Link](i);
i++;
Output:
Example 2: Calculate the sum of the first 5 natural numbers
public class WhileLoopExample2 {
public static void main(String[] args) {
int i = 1, sum = 0;
while (i <= 5) {
sum += i;
i++;
[Link]("Sum: " + sum);
Output:
Sum: 15
Example 3: Reverse a number using a while loop
public class WhileLoopExample3 {
public static void main(String[] args) {
int number = 12345, reversed = 0;
while (number != 0) {
int digit = number % 10;
reversed = reversed * 10 + digit;
number /= 10;
[Link]("Reversed Number: " + reversed);
Output:
Reversed Number: 54321
Exercises:
1. Write a program that prints numbers from 10 to 1 using a while loop.
2. Write a program to find the largest digit in a number using a while loop.
3. Write a program that calculates the sum of digits of a number using a while loop.
3. Do-While Loop Examples
Example 1: Print numbers from 1 to 5 using a do-while loop
public class DoWhileLoopExample1 {
public static void main(String[] args) {
int i = 1;
do {
[Link](i);
i++;
} while (i <= 5);
Output:
Example 2: Calculate the sum of the first 5 natural numbers
public class DoWhileLoopExample2 {
public static void main(String[] args) {
int i = 1, sum = 0;
do {
sum += i;
i++;
} while (i <= 5);
[Link]("Sum: " + sum);
Output:
Sum: 15
Example 3: Keep prompting the user for a password until it is correct
import [Link];
public class DoWhileLoopExample3 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String password;
do {
[Link]("Enter your password: ");
password = [Link]();
} while ();
[Link]("Access granted!");
Output:
Enter your password: 1234
Enter your password: pass
Enter your password: secret
Access granted!
Exercises:
1. Write a program that prints numbers from 20 to 10 using a do-while loop.
2. Write a program that calculates the product of digits of a number using a do-while loop.
3. Write a program that keeps asking the user to enter a number greater than 0 until they do
so, using a do-while loop.