Even/Odd & Swap Two Numbers

Practice the following basic number programs.

Even & Odd Number

Determine whether a number is even or odd using:

if (num % 2 == 0)

If the condition is true, the number is even; otherwise, it is odd.

Advertisement

Positive & Negative Number

Check whether the number is:

  • Positive
  • Negative
  • Zero

using appropriate conditional statements.


Swap Two Numbers

Using a Temporary Variable

temp = a;
a = b;
b = temp;

Without Using a Temporary Variable

a = a + b;
b = a - b;
a = a - b;

Reverse a Number

public class ReverseNumber {
    public static void main(String[] args) {
        int num = 12345;
        int reversed = 0;

        while (num != 0) {
            int digit = num % 10;
            reversed = reversed * 10 + digit;
            num = num / 10;
        }

        System.out.println("Reversed number: " + reversed);
    }
}

Logic

The reverse number program follows a simple digit extraction pattern.

  • Extract the last digit using % 10.
  • Append it to the reversed number.
  • Remove the last digit using / 10.
  • Continue until the number becomes 0.

Count Digits & Factorial

Count Digits

Count the digits by repeatedly dividing the number by 10 until it becomes zero.

Alternative approaches include:

  • String.valueOf(num).length()
  • (int) Math.log10(num) + 1

Factorial

public class FactorialNumber {
    public static void main(String[] args) {
        int num = 5;
        int fact = 1;

        for (int i = 1; i <= num; i++) {
            fact = fact * i;
        }

        System.out.println("Factorial: " + fact);
    }
}

Logic

Factorial is calculated by multiplying all numbers from 1 to the given number.

Example:

5! = 5 × 4 × 3 × 2 × 1 = 120

For larger values, use the long data type to reduce overflow.


Prime Number Check

public class PrimeNumberCheck {
    public static void main(String[] args) {

        int num = 7;
        boolean isPrime = true;

        if (num <= 1) {
            isPrime = false;
        } else {

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

                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }
        }

        System.out.println(num + (isPrime ? " is Prime" : " is not Prime"));
    }
}

Logic

  • Numbers less than or equal to 1 are not prime.
  • Check divisibility starting from 2.
  • If any divisor divides the number exactly, it is not prime.
  • Otherwise, it is a prime number.

Palindrome Number

public class PalindromeNumber {
    public static void main(String[] args) {

        int num = 121;
        int originalNum = num;
        int reversed = 0;

        while (num != 0) {
            int digit = num % 10;
            reversed = reversed * 10 + digit;
            num = num / 10;
        }

        System.out.println(originalNum +
                (reversed == originalNum
                        ? " is a Palindrome"
                        : " is not a Palindrome"));
    }
}

Logic

Reverse the number using the digit extraction approach and compare the reversed value with the original number.

If both values are equal, the number is a palindrome.


Armstrong Number

public class ArmstrongNumber {
    public static void main(String[] args) {

        int num = 153;
        int originalNum = num;
        int result = 0;

        while (num != 0) {

            int digit = num % 10;

            result += Math.pow(digit, 3);

            num = num / 10;
        }

        if (result == originalNum)
            System.out.println(originalNum + " is an Armstrong Number");
        else
            System.out.println(originalNum + " is not an Armstrong Number");
    }
}

Logic

For a three-digit Armstrong number, calculate the sum of the cube of every digit.

Example:

153 = 1³ + 5³ + 3³

If the calculated value equals the original number, it is an Armstrong number.


Perfect Number, Strong Number & Fibonacci Series

Perfect Number

A perfect number is a number whose sum of proper divisors equals the number itself.

Example:

6 = 1 + 2 + 3

Strong Number

A strong number is a number whose sum of the factorials of its digits equals the original number.

Example:

145 = 1! + 4! + 5!

Fibonacci Series

Generate the Fibonacci sequence using two variables.

The sequence starts with:

0, 1

Every subsequent number is the sum of the previous two numbers.


Frequently Asked Questions

How do you reverse a number in Java?

Extract the last digit using % 10, append it to the reversed number, remove the last digit using / 10, and continue until the number becomes zero.


How do you check whether a number is prime?

Check whether the number has any divisor between 2 and num / 2.

If no divisor exists, the number is prime.


What is an Armstrong number?

An Armstrong number is a number equal to the sum of its digits raised to the power of the total number of digits.

Example:

153 = 1³ + 5³ + 3³

How do you check whether a number is a palindrome?

Reverse the number and compare it with the original value.

If both values are equal, it is a palindrome.


What is the difference between a perfect number and a strong number?

  • Perfect Number: Sum of proper divisors equals the number.
  • Strong Number: Sum of the factorials of its digits equals the number.