Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

ProgramsJava

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
  1. 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 →
  2. 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 →
  3. 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 →
  4. 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 →
  5. 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 →
  6. 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 →
  7. 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 →
  8. 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 →
  9. 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 →
  10. 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 →
  11. 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 →
  12. 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 →
  13. 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 →
  14. 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 →
  15. 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 →
  16. 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 →
  17. 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 →
  18. 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 →
  19. 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 →
  20. 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 →
  21. 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 →
  22. 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 →
  23. 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 →
  24. 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 →
  25. 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 →