Example 1: Checking if a Number is Even or Odd

import java.util.Scanner;

public class EvenOdd {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        System.out.print("Enter a number: ");

        int number = scanner.nextInt();

        if (number % 2 == 0) {

            System.out.println(number + " is even.");

        } else {

            System.out.println(number + " is odd.");

        }

    }

}

Output:

Enter a number: 7

7 is odd.


Example 2: Checking if a Year is a Leap Year

import java.util.Scanner;

public class LeapYear {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a year: ");

        int year = scanner.nextInt();

        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

            System.out.println(year + " is a leap year.");

        } else {

            System.out.println(year + " is not a leap year.");

        }

    }

}

Output:

Enter a year: 2024

2024 is a leap year.


Example 3: Finding the Maximum of Two Numbers

import java.util.Scanner;

public class MaxNumber {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first number: ");

        int num1 = scanner.nextInt();

        System.out.print("Enter the second number: ");

        int num2 = scanner.nextInt();

        int max = (num1 > num2) ? num1 : num2;

        System.out.println("The maximum number is: " + max);

    }

}

Output:

Enter the first number: 25

Enter the second number: 19

The maximum number is: 25


Example 4: Printing Numbers from 1 to 10 Using a For Loop

public class PrintNumbers {

    public static void main(String[] args) {

        for (int i = 1; i <= 10; i++) {

            System.out.print(i + " ");

        }

    }

}

Output:

1 2 3 4 5 6 7 8 9 10


Example 5: Calculating the Factorial of a Number Using a While Loop

import java.util.Scanner;

public class Factorial {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        System.out.print("Enter a number: ");

        int n = scanner.nextInt();

        int factorial = 1;

        int i = 1;

        while (i <= n) {

            factorial *= i;

            i++;

        }

        System.out.println("Factorial of " + n + " is: " + factorial);

    }

}

Output:

Enter a number: 5

Factorial of 5 is: 120


Example 6: Summing Numbers from 1 to 5 Using a Do-While Loop

public class SumNumbers {

    public static void main(String[] args) {

        int sum = 0;

        int i = 1;


        do {

            sum += i;

            i++;

        } while (i <= 5);

        System.out.println("Sum of numbers from 1 to 5 is: " + sum);

    }

}

Output:

Sum of numbers from 1 to 5 is: 15


Example 7: Checking Prime Numbers Using a For Loop

import java.util.Scanner;

public class PrimeNumber {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int num = scanner.nextInt();

        boolean isPrime = true;

        for (int i = 2; i <= num / 2; i++) {

            if (num % i == 0) {

                isPrime = false;

                break;

            }

        }

        if (isPrime) {

            System.out.println(num + " is a prime number.");

        } else {

            System.out.println(num + " is not a prime number.");

        }

    }

}

Output:

Enter a number: 11

11 is a prime number.


Example 8: Printing a Pattern Using Nested For Loops

public class Pattern {

    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();

        }

    }

}

Output:

* * 

* * * 

* * * * 

* * * * *


Example 9: Counting the Number of Digits in a Number Using a While Loop

import java.util.Scanner;

public class CountDigits {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        System.out.print("Enter a number: ");

        int num = scanner.nextInt();

        int count = 0;


        while (num != 0) {

            num /= 10;

            count++;

        }


        System.out.println("Number of digits: " + count);

    }

}

Output:

Enter a number: 4567

Number of digits: 4


Example 10: Calculating the Sum of Digits in a Number Using a For Loop

import java.util.Scanner;

public class SumDigits {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int num = scanner.nextInt();

        int sum = 0;

        for (; num != 0; num /= 10) {

            sum += num % 10;

        }

        System.out.println("Sum of digits: " + sum);

    }

}

Output:

Enter a number: 123

Sum of digits: 6


If you have any question then comment below - We will answer your questions. And do not forget to follow us.