Module 3: Loop Programs
for and while. Digits, primes, patterns. You will get tired of i++ and that is the point.
25 programs · open one, then the next
Start with Print 1–100- 01Print 1–100BeginnerPrint numbers from 1 to 100 using a loop.Open example →javapublic class Main { public static void main(String[] args) { for (int i = 1; i <= 100; i++) { System.out.print(i + " ");
- 02Print Even NumbersBeginnerPrint all even numbers in a given range.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 NumbersBeginnerPrint all odd numbers in a given range.Open example →javapublic class Main { public static void main(String[] args) { for (int i = 1; i <= 100; i += 2) { System.out.print(i + " ");
- 04FactorialBeginnerFind factorial of a number using a loop.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 05Reverse a NumberBeginnerReverse the digits of a number using a loop.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 06Sum of DigitsBeginnerCompute the sum of digits of a number.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 07Count DigitsBeginnerCount the number of digits in a given integer.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 08Armstrong Numbers in RangeIntermediatePrint all Armstrong numbers in a given range.Open example →javapublic class Main { private static boolean isArmstrong(int n) { int temp = n; int digits = 0;
- 09Prime Numbers in RangeIntermediatePrint all prime numbers in a given range.Open example →javapublic class Main { private static boolean isPrime(int n) { if (n <= 1) return false; if (n == 2) return true;
- 10Fibonacci SeriesBeginnerPrint Fibonacci series up to N terms using a loop.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 11GCD (HCF)IntermediateFind the greatest common divisor (HCF) of two numbers using Euclidean algorithm.Open example →javaimport java.util.Scanner; public class Main { private static int gcd(int a, int b) {
- 12LCMIntermediateFind the least common multiple of two numbers using GCD.Open example →javaimport java.util.Scanner; public class Main { private static int gcd(int a, int b) {
- 13Power of NumberBeginnerCompute a^b using a loop (similar to earlier power program).Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 14Sum of SeriesBeginnerCompute the sum of the series 1 + 2 + ... + n using a loop.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 15Number PatternsBeginnerPrint a simple increasing number triangle pattern.Open example →javapublic class Main { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) {
- 16Star PatternsBeginnerPrint a simple right-angled triangle star pattern.Open example →javapublic class Main { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) {
- 17Floyd TriangleBeginnerPrint Floyd’s triangle pattern using consecutive numbers.Open example →javapublic class Main { public static void main(String[] args) { int n = 5; int num = 1;
- 18Pascal TriangleIntermediateGenerate Pascal’s triangle using loops and binomial coefficients.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 the multiplication table of a given number.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 20Factor PairsIntermediatePrint all factor pairs (i, n/i) of a given number.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 21Prime CheckIntermediateCheck whether a given number is prime using a loop.Open example →javaimport java.util.Scanner; public class Main { private static boolean isPrime(int n) {
- 22Composite CheckIntermediateCheck whether a given number is composite.Open example →javaimport java.util.Scanner; public class Main { private static boolean isComposite(int n) {
- 23Perfect Square CheckBeginnerCheck whether a number is a perfect square using loops.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 24Harshad NumberIntermediateCheck whether a number is a Harshad (Niven) number (divisible by sum of its digits).Open example →javaimport java.util.Scanner; public class Main { private static int sumOfDigits(int n) {
- 25Duck NumberIntermediateCheck whether a number is a duck number (contains zero, but not at the first position).Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {