Java Number Programs with Output - Frequently Asked Interview Programs

This tutorial shows 10 Java programs on a number for beginners to practice.

Here are 10 Java number programs for practice:
  1. Java program to check prime number
  2. Java Program to Swap Two Numbers Using a Temporary Variable
  3. Java Program to Swap Two Numbers Without Using a Temporary Variable
  4. Java Program to Check if Number is Positive or Negative
  5. Java Program to Check Armstrong Number
  6. Java Program to Find GCD of Two Numbers
  7. Java Program to Find Largest of Three Numbers
  8. Java Program to Find Factorial of a Number
  9. Java Program to Multiply Two Numbers
  10. Java Program to find the Smallest of three numbers using Ternary Operator

1. Java program to check prime number

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java program to check prime number
 * 
 * @author https://www.javaguides.net/
 *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            boolean isPrime = true;
            System.out.println("Enter any number:");
            // capture the input in an integer
            int num = scanner.nextInt();
            for (int i = 2; i <= num / 2; i++) {
                int temp = num % i;
                if (temp == 0) {
                    isPrime = false;
                    break;
                }
            }
            // If isPrime is true then the number is prime else not
            if (isPrime) {
                System.out.println(num + " is a Prime Number");
            } else {
                System.out.println(num + " is not a Prime Number");
            }
        }
    }
}
Output:
Enter any number:
17
17 is a Prime Number

2. Java Program to Swap Two Numbers Using a Temporary Variable

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to Swap Two Numbers
 * 
 * @author https://www.javaguides.net/ *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {

            System.out.print("Enter first number:");
            int first = scanner.nextInt();
            System.out.print("Enter second number:");
            int second = scanner.nextInt();

            System.out.println("--Before swap--");
            System.out.println("First number = " + first);
            System.out.println("Second number = " + second);

            // Value of first is assigned to temporary
            int temporary = first;

            // Value of second is assigned to first
            first = second;

            // Value of temporary (which contains the initial value of first) is assigned to
            // second
            second = temporary;

            System.out.println("--After swap--");
            System.out.println("First number = " + first);
            System.out.println("Second number = " + second);
        }
    }
}
Output:
Enter first number:10
Enter second number:20
--Before swap--
First number = 10
Second number = 20
--After swap--
First number = 20
Second number = 10

3. Java Program to Swap Two Numbers Without Using a Temporary Variable

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to Swap Two Numbers
 * 
 * @author https://www.javaguides.net/ *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {

            System.out.print("Enter first number:");
            int first = scanner.nextInt();
            System.out.print("Enter second number:");
            int second = scanner.nextInt();

            System.out.println("--Before swap--");
            System.out.println("First number = " + first);
            System.out.println("Second number = " + second);

            first = first - second;
            second = first + second;
            first = second - first;

            System.out.println("--After swap--");
            System.out.println("First number = " + first);
            System.out.println("Second number = " + second);
        }
    }
}
Output:
Enter first number:10
Enter second number:20
--Before swap--
First number = 10
Second number = 20
--After swap--
First number = 20
Second number = 10

4. Java Program to Check if Number is Positive or Negative

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to check if Number is Positive or Negative
 * 
 * @author https://www.javaguides.net/ *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter the number you want to check:");
            int number = scanner.nextInt();
            if (number > 0) {
                System.out.println(number + " is positive number");
            } else if (number < 0) {
                System.out.println(number + " is negative number");
            } else {
                System.out.println(number + " is neither positive nor negative");
            }
        }
    }
}
Output:
Enter the number you want to check:-5
-5 is negative number

5. Java Program to Check Armstrong Number

Armstrong Number in Java: A positive number is called Armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.
Let's try to understand why 153 is an Armstrong number.
153 = (1*1*1)+(5*5*5)+(3*3*3)  
where:  
(1*1*1)=1  
(5*5*5)=125  
(3*3*3)=27  
So:  
1+125+27=153 
Here is a Java program:
package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to Check Armstrong Number
 * 
 * @author https://www.javaguides.net/ *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            int temp, total = 0;
            System.out.println("Ënter 3 Digit Number : ");
            int num = scanner.nextInt();
            int number = num;

            for (; number != 0; number /= 10) {
                temp = number % 10;
                total = total + temp * temp * temp;
            }

            if (total == num) {
                System.out.println(num + " is an Armstrong number");
            } else {
                System.out.println(num + " is not an Armstrong number");
            }
        }
    }
}
Output:
Ënter 3 Digit Number : 
153
153 is an Armstrong number

6. Java Program to Find GCD of Two Numbers

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to Find GCD of Two Numbers
 * 
 * @author https://www.javaguides.net/ *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            int num1, num2;
            System.out.print("Enter first number:");
            num1 = (int) scanner.nextInt();

            System.out.print("Enter second number:");
            num2 = (int) scanner.nextInt();

            // closing the scanner to avoid memory leaks
            scanner.close();
            while (num1 != num2) {
                if (num1 > num2)
                    num1 = num1 - num2;
                else
                    num2 = num2 - num1;
            }

            // displaying the result
            System.out.printf("GCD of given numbers is: %d", num2);
        }
    }
}
Output:
Enter first number:10
Enter second number:20
GCD of given numbers is: 10

7. Java Program to Find Largest of Three Numbers

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to find largest of three Numbers
 * 
 * @author https://www.javaguides.net/ *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            int num1, num2, num3;
            System.out.print("Enter first number:");
            num1 = (int) scanner.nextInt();

            System.out.print("Enter first number:");
            num2 = (int) scanner.nextInt();

            System.out.print("Enter first number:");
            num3 = (int) scanner.nextInt();

            if (num1 >= num2 && num1 >= num3) {
                System.out.println(num1 + " is the largest Number");
            } else if (num2 >= num1 && num2 >= num3) {
                System.out.println(num2 + " is the largest Number");
            } else {
                System.out.println(num3 + " is the largest Number");
            }
        }
    }
}
Output:
Enter first number:50
Enter first number:20
Enter first number:60
60 is the largest Number

8. Java Program to Find Factorial of a Number

In the below program, we are using a Scanner to get input from the console.
import java.math.BigInteger;
import java.util.Scanner;

public class FactorialExample {

    public static void main(String[] args) {

     Scanner scanner = new Scanner(System.in);

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

        int n = scanner.nextInt();
        BigInteger fact = factorial(n);

        System.out.printf("%d! is %d", n, fact);
        
        scanner.close();
    }

    private static BigInteger factorial(int value) {

     BigInteger result = BigInteger.ONE;

        if (value != 0 && value != 1) {

            for (int i = 2; i <= value; i++) {

                result = result.multiply(BigInteger.valueOf(i));
            }
        }

        return result;
    }
}
Output:
Enter a number: 5

5! is 120

9. Java Program to Multiply Two Numbers

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to check Even or Odd number
 * 
 * @author https://www.javaguides.net/ *
 */
public class JavaProgramMultiplyNumber {
    public static void main(String[] args) {

        try (Scanner sc = new Scanner(System.in)) {

            // Read number from user
            System.out.println("Enter Number: ");
            int number = sc.nextInt();
            /*
             * If number is divisible by 2 then it's an even number else odd number
             */
            if (number % 2 == 0) {
                System.out.println("The number " + number + " is even");
            } else {
                System.out.println("The number " + number + " is odd");
            }
        }
    }
}
Output:
Enter first number: 10
Enter second number: 20
Output: 200

10. Java Program to find the Smallest of three numbers using Ternary Operator

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to find the Smallest of three numbers using Ternary Operator
 * 
 * @author https://www.javaguides.net/ *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            int num1, num2, num3, temp;
            System.out.print("Enter first number:");
            num1 = (int) scanner.nextInt();

            System.out.print("Enter Second number:");
            num2 = (int) scanner.nextInt();

            System.out.print("Enter Third number:");
            num3 = (int) scanner.nextInt();

            temp = num1 < num2 ? num1 : num2;
            int result = num3 < temp ? num3 : temp;
            System.out.println("Smallest Number is:" + result);
        }
    }
}
Output:
Enter first number:10
Enter Second number:2
Enter Third number:3
Smallest Number is:2

Related Java Programs


Comments