1. While Loop practice program-1
This program prints numbers from 1 to the given value of N. It uses a while loop to repeat until the condition becomes false.
import java.util.Scanner;
public class PrintNumbersWhile {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter N: ");
int n = sc.nextInt();
int i = 1;
while (i <= n) {
System.out.print(i + " ");
i++;
}
sc.close();
}
}Input and Output:
Enter N: 5
1 2 3 4 5
2. While Loop practice program-2
This program calculates the sum of the first N natural numbers. It repeatedly adds each number using a while loop.
import java.util.Scanner;
public class SumWhile {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter N: ");
int n = sc.nextInt();
int i = 1, sum = 0;
while (i <= n) {
sum += i;
i++;
}
System.out.println("Sum = " + sum);
sc.close();
}
}Input and Output:
Enter N: 5
Sum = 15
3. Do-While Loop practice program-1
This program prints numbers from 1 to N using a do-while loop. The loop executes at least once before checking the condition.
import java.util.Scanner;
public class PrintNumbersDoWhile {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter N: ");
int n = sc.nextInt();
int i = 1;
do {
System.out.print(i + " ");
i++;
} while (i <= n);
sc.close();
}
}Input and Output:
Enter N: 5
1 2 3 4 5
4. Do-While Loop practice program-2
This program reverses the digits of an integer. It repeatedly extracts the last digit using a do-while loop.
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number: ");
int num = sc.nextInt();
int reverse = 0;
do {
int digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
} while (num != 0);
System.out.println("Reverse = " + reverse);
sc.close();
}
} Input and Output:
Enter Number: 54321
Reverse = 12345Code language: JavaScript (javascript)
5. For Loop practice program-1
This program displays the multiplication table of a given number. It uses a for loop to generate multiples from 1 to 10.
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number: ");
int n = sc.nextInt();
for (int i = 1; i <= 10; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
sc.close();
}
}Input and Output:
Enter Number: 5
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 = 50Code language: JavaScript (javascript)
6. For Loop practice program-2
This program calculates the factorial of a given number. It multiplies numbers from 1 to N using a for loop.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number: ");
int n = sc.nextInt();
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
System.out.println("Factorial = " + fact);
sc.close();
}
}Input and Output:
Enter Number: 5
Factorial = 120Code language: JavaScript (javascript)
7. For Loop practice program-3
This program checks whether the entered number is prime. It uses a for loop to test divisibility.
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number: ");
int n = sc.nextInt();
boolean prime = true;
if (n <= 1) {
prime = false;
} else {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
prime = false;
break;
}
}
}
if (prime)
System.out.println("Prime Number");
else
System.out.println("Not a Prime Number");
sc.close();
}
}Input and Output:
Enter Number: 3
Prime NumberCode language: JavaScript (javascript)
8. For Loop practice program-4
This program prints a right-angled triangle star pattern. It uses nested for loops to generate rows and columns.
public class StarPattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}Output:
*
* *
* * *
* * * *
* * * * *
9. For-Each Loop practice program-1
This program finds the sum of all elements in an array. It uses the enhanced for-each loop to access each element.
public class SumArray {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int sum = 0;
for (int num : numbers) {
sum += num;
}
System.out.println("Sum = " + sum);
}
}Output:
Sum = 150
10. For-Each Loop practice program-2
This program finds the largest value in an array. It compares each element using the enhanced for-each loop.
public class LargestArray {
public static void main(String[] args) {
int[] numbers = {25, 40, 18, 90, 65};
int largest = numbers[0];
for (int num : numbers) {
if (num > largest) {
largest = num;
}
}
System.out.println("Largest Element = " + largest);
}
}Output:
Largest Element = 90