Java Loop Programs
Practise Java loop programs for factorials, number series, prime checks, and patterns. Follow the changing values with full code and explained output.
25 programs · open one, then the next
Start with Print 1–100- 01Print 1–100BeginnerPrint 1 through 100 in Java with a for loop, i++, and System.out.print. Spaces separate the numbers on one line; <= includes the final value of 100.Open example →javapublic class Main { public static void main(String[] args) { for (int i = 1; i <= 100; i++) { System.out.print(i + " ");
- 02Print Even NumbersBeginnerList even numbers in Java by starting a for loop at 2 and stepping by 2. The one-line output contains fifty values through 100, deliberately leaving out zero.Open example →javapublic class Main { public static void main(String[] args) { for (int i = 2; i <= 100; i += 2) { System.out.print(i + " ");
- 03Print Odd NumbersBeginnerShow odd numbers from 1 to 99 in Java with a for loop and i += 2. The limit is 100, but the last printed value is 99 because the next step reaches 101.Open example →javapublic class Main { public static void main(String[] args) { for (int i = 1; i <= 100; i += 2) { System.out.print(i + " ");
- 04FactorialBeginnerFind a factorial in Java with Scanner and a for loop that multiplies into long. The guard accepts 0–20 only: 0! is 1, while 21! exceeds the long range.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 05Reverse a NumberBeginnerReverse integer digits in Java using % 10, division, and a long accumulator. Scanner reads an int; trailing zeros disappear and negative signs remain.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 06Sum of DigitsBeginnerAdd a number's digits in Java by taking % 10 and dividing by ten in a while loop. Convert Scanner input to long before Math.abs to handle every int safely.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 07Count DigitsBeginnerCount decimal digits in Java with repeated division by ten after Scanner input. Zero counts as one digit; minus signs and typed leading zeros do not count.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 08Armstrong Numbers in RangeIntermediateList Armstrong numbers from 1 to 1000 in Java with an isArmstrong helper and nested digit-power loops. Every positive one-digit number also qualifies.Open example →javapublic class Main { private static boolean isArmstrong(int n) { int temp = n; int digits = 0;
- 09Prime Numbers in RangeIntermediateFind primes from 1 to 100 in Java with an isPrime helper that tests odd divisors through the square root. Two is included, while one is not prime.Open example →javapublic class Main { private static boolean isPrime(int n) { if (n <= 1) return false; if (n == 2) return true;
- 10Fibonacci SeriesBeginnerGenerate Fibonacci terms in Java by updating two integers in a for loop after Scanner input. Seven terms end at 8; large terms can overflow int.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 11GCD (HCF)IntermediateFind the GCD or HCF in Java by replacing a pair with b and a % b until b is zero. Scanner supplies the values; this version expects non-negative inputs.Open example →javaimport java.util.Scanner; public class Main { private static int gcd(int a, int b) {
- 12LCMIntermediateCalculate LCM in Java using a remainder-based GCD helper and a long product. Divide before multiplying; zero input returns zero without dividing by zero.Open example →javaimport java.util.Scanner; public class Main { private static int gcd(int a, int b) {
- 13Power of NumberBeginnerRaise an integer to a power in Java by multiplying a long accumulator once per loop pass. An exponent of zero returns 1; negative exponents need other logic.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 14Sum of SeriesBeginnerAdd the series 1 through n in Java with sum += i inside a for loop. Scanner sets the limit; non-positive n prints zero, and large totals may overflow int.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 15Number PatternsBeginnerPrint a five-row number triangle in Java using nested for loops. Restart the inner counter at 1 each row; print a newline only after the row is complete.Open example →javapublic class Main { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) {
- 16Star PatternsBeginnerDraw a growing star triangle in Java with nested loops and System.out.print. Row five contains five stars; println after each inner loop keeps the shape.Open example →javapublic class Main { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) {
- 17Floyd TriangleBeginnerBuild Floyd's triangle in Java with nested loops and a counter kept outside both loops. Post-increment continues across rows; five rows finish at 15.Open example →javapublic class Main { public static void main(String[] args) { int n = 5; int num = 1;
- 18Pascal TriangleIntermediateDisplay five rows of Pascal's triangle in Java using an nCr helper with product and division steps. Rows are left-aligned; larger rows can overflow int.Open example →javapublic class Main { private static int nCr(int n, int r) { int res = 1; for (int i = 0; i < r; i++) {
- 19Multiplication TableBeginnerPrint a multiplication table in Java with Scanner and a for loop from 1 to 10. Each line calculates n * i; large input products may need a wider type.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 20Factor PairsIntermediateList factor pairs in Java with % and the safe i <= n / i loop limit. Scanner reads a positive integer; each pair prints once, including equal square factors.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 21Prime CheckIntermediateDecide if an integer is prime in Java with an isPrime helper and odd-divisor checks. Use i <= n / i to avoid overflow; one and negatives are rejected.Open example →javaimport java.util.Scanner; public class Main { private static boolean isPrime(int n) {
- 22Composite CheckIntermediateDetect a composite number in Java by searching for a divisor with % up to i <= n / i. A match ends the loop early; zero and one are not composite.Open example →javaimport java.util.Scanner; public class Main { private static boolean isComposite(int n) {
- 23Perfect Square CheckBeginnerSearch for a perfect square in Java by trying integers and comparing (long) i * i with the input. Zero matches, negative inputs fail, and break stops a match.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 24Harshad NumberIntermediateTest a Harshad number in Java with a digit-sum helper and n % sum. Positive inputs must divide exactly by their digit sum; guards reject zero and negatives.Open example →javaimport java.util.Scanner; public class Main { private static int sumOfDigits(int n) {
- 25Duck NumberIntermediateCheck a duck number in Java by reading text, validating digits, and scanning for a later zero. Leading zeros stay visible and disqualify the input.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {