Skip to the content.

Java > 🧑‍💻 Loops Basics Exercise

Problem 1: Print Numbers using a For Loop

Description: Write a Java program that prints numbers from 1 to N using a for loop.

Example:

Input: Take an integer N from the user (e.g., 5)
Output: 1 2 3 4 5

Problem 2: Sum of N Natural Numbers

Description: Write a Java program to find the sum of the first N natural numbers using a for loop.

Example:

Input: Take an integer N from the user (e.g., 5)
Output: Sum = 15

Problem 3: Multiplication Table Generator

Description: Write a Java program that prints the multiplication table of a given number using a for loop.

Example:

Input: Take an integer num from the user (e.g., 3)
Output:
3 x 1 = 3
3 x 2 = 6
...
3 x 10 = 30

Problem 4: Reverse a Number using a While Loop

Description: Write a Java program that reverses a given number using a while loop.

Example:

Input: Take an integer num from the user (e.g., 1234)
Output: Reversed Number: 4321

Problem 5: Check for Prime Number using a For Loop

Description: Write a Java program to check if a number is prime using a for loop.

Example:

Input: Take an integer num from the user (e.g., 7)
Output: 7 is a prime number

Problem 6: Factorial Calculation using a For Loop

Description: Write a Java program to calculate the factorial of a number using a for loop.

Example:

Input: Take an integer num from the user (e.g., 5)
Output: Factorial = 120

Problem 7: Fibonacci Series using a While Loop

Description: Write a Java program that prints the first N Fibonacci numbers using a while loop.

Example:

Input: Take an integer N from the user (e.g., 7)
Output: 0 1 1 2 3 5 8

Problem 8: Count Digits in a Number using a While Loop

Description: Write a Java program to count the number of digits in an integer using a while loop.

Example:

Input: Take an integer num from the user (e.g., 12345)
Output: Number of digits: 5

Problem 9: Palindrome Checker using a While Loop

Description: Write a Java program that checks whether a given number is a palindrome using a while loop.

Example:

Input: Take an integer num from the user (e.g., 121)
Output: 121 is a palindrome number

Problem 10: Pattern Printing using a For Loop

Description: Write a Java program that prints a right-angled triangle pattern of stars using a for loop.

Example:

Input: Take an integer N from the user (e.g., 5)
Output:
*
**
***
****
*****