Loops are one of the most powerful concepts in Java programming. They allow you to execute the same block of code repeatedly without writing it multiple times. Instead of manually repeating statements, loops automate repetitive tasks, making programs shorter, cleaner, and more efficient.
Loops are used in almost every Java application, including:
- Data Processing
- Banking Software
- Student Management Systems
- E-commerce Applications
- Game Development
- Android Applications
- Enterprise Software
- Automation Scripts
- Report Generation
Java provides three types of loops:
forLoopwhileLoopdo-whileLoop
These loops are used depending on the problem requirements. Understanding loops is essential before learning arrays, methods, object-oriented programming, collections, and advanced Java concepts.
In this chapter, you’ll solve beginner-friendly and interview-oriented Java loop practice questions. Each question includes a complete Java solution, sample input/output, explanation, and concepts covered to help you master Java loops. Java Loops practice questions with solutions help to understand the concepts.
1. Java Program to Print Numbers from 1 to 10 Using a for Loop
Problem Statement
Write a Java program to print numbers from 1 to 10 using a for loop.
Java Solution
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
Sample Output
1
2
3
4
5
6
7
8
9
10
Explanation
The loop starts from 1 and continues until 10.
- Initialization →
i = 1 - Condition →
i <= 10 - Increment →
i++
The loop executes 10 times.
Concepts Covered
- for Loop
- Loop Initialization
- Loop Condition
- Increment Operator
2. Java Program to Print Even Numbers from 1 to 100
Problem Statement
Write a Java program to print all even numbers between 1 and 100.
Java Solution
public class Main {
public static void main(String[] args) {
for (int i = 2; i <= 100; i += 2) {
System.out.println(i);
}
}
}
Sample Output
2
4
6
8
...
100
Explanation
The loop starts at 2 and increases by 2 each time, ensuring that only even numbers are printed.
Concepts Covered
- for Loop
- Increment by 2
- Even Numbers
3. Java Program to Print Odd Numbers from 1 to 100
Problem Statement
Write a Java program to print all odd numbers between 1 and 100.
Java Solution
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 100; i += 2) {
System.out.println(i);
}
}
}
Sample Output
1
3
5
7
...
99
Explanation
The loop begins at 1 and increments by 2, printing only odd numbers.
Concepts Covered
- for Loop
- Odd Numbers
- Loop Increment
4. Java Program to Print Numbers in Reverse Order
Problem Statement
Write a Java program to print numbers from 10 to 1 in reverse order using a for loop.
Java Solution
public class Main {
public static void main(String[] args) {
for (int i = 10; i >= 1; i--) {
System.out.println(i);
}
}
}
Sample Output
10
9
8
7
6
5
4
3
2
1
Explanation
The loop starts from 10 and decreases by 1 in every iteration until it reaches 1.
- Initialization →
i = 10 - Condition →
i >= 1 - Decrement →
i--
This is useful when numbers need to be processed in reverse order.
Concepts Covered
- for Loop
- Decrement Operator
- Reverse Counting
5. Java Program to Calculate the Sum of First N Natural Numbers
Problem Statement
Write a Java program to calculate the sum of the first N natural numbers using a for loop.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
int sum = 0;
System.out.print("Enter a Number: ");
n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
sum = sum + i;
}
System.out.println("Sum = " + sum);
scanner.close();
}
}
Sample Input
Enter a Number: 10
Sample Output
Sum = 55
Explanation
The program starts from 1 and adds each number to the sum variable until it reaches N.
For N = 10:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
Concepts Covered
- for Loop
- Accumulator Variable
- Natural Numbers
- Arithmetic Operations
6. Java Program to Print the Multiplication Table of a Number
Problem Statement
Write a Java program to print the multiplication table of a given number up to 10 using a for loop.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.print("Enter a Number: ");
number = scanner.nextInt();
System.out.println("\nMultiplication Table of " + number);
for (int i = 1; i <= 10; i++) {
System.out.println(number + " x " + i + " = " + (number * i));
}
scanner.close();
}
}
Sample Input
Enter a Number: 7
Sample Output
Multiplication Table of 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Explanation
The loop runs from 1 to 10, multiplying the entered number by the loop variable in each iteration.
Concepts Covered
- for Loop
- Multiplication
- User Input
- Arithmetic Operations
7. Java Program to Calculate the Factorial of a Number
Problem Statement
Write a Java program to calculate the factorial of a given number.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
long factorial = 1;
System.out.print("Enter a Number: ");
number = scanner.nextInt();
for (int i = 1; i <= number; i++) {
factorial = factorial * i;
}
System.out.println("Factorial = " + factorial);
scanner.close();
}
}
Sample Input
Enter a Number: 5
Sample Output
Factorial = 120
Explanation
Factorial of a number is the product of all positive integers from 1 to that number.
Example:
5! = 5 × 4 × 3 × 2 × 1 = 120
Concepts Covered
- for Loop
- Factorial
- Arithmetic Operations
8. Java Program to Count the Number of Digits in an Integer
Problem Statement
Write a Java program to count the number of digits present in an integer.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
int count = 0;
System.out.print("Enter a Number: ");
number = scanner.nextInt();
while (number != 0) {
number = number / 10;
count++;
}
System.out.println("Total Digits = " + count);
scanner.close();
}
}
Sample Input
Enter a Number: 987654
Sample Output
Total Digits = 6
Explanation
The program repeatedly divides the number by 10 until it becomes 0.
Each division removes one digit, and the counter increases.
Concepts Covered
- while Loop
- Integer Division
- Counting Digits
9. Java Program to Reverse a Number
Problem Statement
Write a Java program to reverse the digits of a number.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
int reverse = 0;
System.out.print("Enter a Number: ");
number = scanner.nextInt();
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
System.out.println("Reverse Number = " + reverse);
scanner.close();
}
}
Sample Input
Enter a Number: 12345
Sample Output
Reverse Number = 54321
Explanation
The program extracts the last digit using % 10, appends it to the reversed number, and removes the last digit using / 10.
Concepts Covered
- while Loop
- Modulus Operator
- Reverse Number Logic
10. Java Program to Check Whether a Number is a Palindrome
Problem Statement
Write a Java program to check whether a given number is a palindrome.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
int originalNumber;
int reverse = 0;
System.out.print("Enter a Number: ");
number = scanner.nextInt();
originalNumber = number;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
if (originalNumber == reverse) {
System.out.println("Palindrome Number");
} else {
System.out.println("Not a Palindrome Number");
}
scanner.close();
}
}
Sample Input
Enter a Number: 121
Sample Output
Palindrome Number
Explanation
A palindrome number remains the same when read from left to right and right to left.
Examples:
- 121
- 1331
- 777
The program reverses the number and compares it with the original.
Concepts Covered
- while Loop
- Reverse Number
- if-else
- Palindrome Logic
11. Java Program to Check Whether a Number is Prime
Problem Statement
Write a Java program to determine whether a given number is a prime number.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
boolean isPrime = true;
System.out.print("Enter a Number: ");
number = scanner.nextInt();
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println(number + " is a Prime Number.");
} else {
System.out.println(number + " is Not a Prime Number.");
}
scanner.close();
}
}
Sample Input
Enter a Number: 17
Sample Output
17 is a Prime Number.
Explanation
A prime number has exactly two factors:
- 1
- Itself
The program checks divisibility from 2 to number / 2. If any divisor is found, the number is not prime.
Concepts Covered
- for Loop
- Prime Number Logic
- Boolean Variable
- break Statement
12. Java Program to Print Prime Numbers Between 1 and N
Problem Statement
Write a Java program to print all prime numbers between 1 and N.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int limit;
System.out.print("Enter Limit: ");
limit = scanner.nextInt();
System.out.println("Prime Numbers:");
for (int number = 2; number <= limit; number++) {
boolean isPrime = true;
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(number + " ");
}
}
scanner.close();
}
}
Sample Input
Enter Limit: 20
Sample Output
Prime Numbers:
2 3 5 7 11 13 17 19
Explanation
The outer loop checks every number from 2 to N, while the inner loop verifies whether the current number is prime.
Concepts Covered
- Nested Loops
- Prime Numbers
- Boolean Variables
13. Java Program to Generate Fibonacci Series
Problem Statement
Write a Java program to generate the Fibonacci series up to N terms.
Java Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int terms;
System.out.print("Enter Number of Terms: ");
terms = scanner.nextInt();
int first = 0;
int second = 1;
System.out.println("Fibonacci Series:");
for (int i = 1; i <= terms; i++) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
scanner.close();
}
}
Sample Input
Enter Number of Terms: 8
Sample Output
Fibonacci Series:
0 1 1 2 3 5 8 13
Explanation
Each Fibonacci number is obtained by adding the previous two numbers.
Example:
0 1 1 2 3 5 8 13 ...
Concepts Covered
- for Loop
- Fibonacci Logic
- Variables
14. Java Program to Print Star Pattern
Problem Statement
Write a Java program to print the following star pattern.
*
**
***
****
*****
Java Solution
public class Main {
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();
}
}
}
Sample Output
*
**
***
****
*****
Explanation
The outer loop controls the number of rows, while the inner loop prints the required number of stars in each row.
Concepts Covered
- Nested Loops
- Pattern Printing
- for Loop
15. Java Program to Print Number Pattern
Problem Statement
Write a Java program to print the following number pattern.
1
12
123
1234
12345
Java Solution
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
Sample Output
1
12
123
1234
12345
Explanation
The outer loop controls the rows, and the inner loop prints numbers from 1 up to the current row number.
Concepts Covered
- Nested Loops
- Number Pattern
- Pattern Printing
- for Loop
Chapter Summary
In this chapter, you learned how Java Loops help execute a block of code repeatedly without writing the same statements multiple times. Loops are one of the most important building blocks of Java programming because they simplify repetitive tasks and make programs more efficient.
You practiced solving real-world Java programs using:
forLoopwhileLoop- Nested Loops
You also implemented common interview questions such as:
- Printing Numbers
- Even and Odd Numbers
- Reverse Counting
- Sum of Natural Numbers
- Multiplication Tables
- Factorial
- Counting Digits
- Reverse Number
- Palindrome Number
- Prime Number
- Fibonacci Series
- Star Patterns
- Number Patterns
These programs improve logical thinking and prepare you for technical interviews as well as real-world Java development.
Key Takeaways
- Loops execute a block of code repeatedly.
- Java provides three types of loops:
forwhiledo-while
forloops are ideal when the number of iterations is known.whileloops are useful when the number of iterations is unknown.- Nested loops are commonly used for pattern printing.
- Loop control statements improve program efficiency.
- Prime number and Fibonacci logic are common coding interview questions.
- Pattern printing strengthens nested loop concepts.
- Loops are heavily used in arrays, collections, file handling, and algorithms.
- Mastering loops is essential before learning arrays and methods.
Frequently Asked Questions (FAQs)
1. What is a loop in Java?
A loop repeatedly executes a block of code until a specified condition becomes false.
2. How many types of loops are available in Java?
Java provides three loop statements:
forwhiledo-while
3. When should we use a for loop?
Use a for loop when the number of iterations is already known.
Example:
for(int i = 1; i <= 10; i++)
{
System.out.println(i);
}
4. What is the difference between while and do-while?
| while | do-while |
|---|---|
| Condition checked before execution | Condition checked after execution |
| May execute zero times | Executes at least once |
5. What is a nested loop?
A nested loop is a loop inside another loop.
It is mainly used for:
- Pattern Printing
- Matrix Problems
- Table Generation
6. Which loop is best for pattern printing?
The for loop with nested loops is the most commonly used approach for printing star and number patterns.
7. Why is the break statement used inside loops?
The break statement immediately terminates the loop when a specific condition is met.
8. Why are Java loops important?
Loops are used in almost every Java application for:
- Data Processing
- Searching
- Sorting
- File Handling
- Collections
- Database Operations
- Automation
- Game Development
- Android Development
- Enterprise Applications
They are among the most frequently asked topics in Java coding interviews.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
