---
title: Basics Java Programs
description: "**Frequently Asked Basics Java Programs** ## **Check if a Number is Even or Odd? - Program 1** ***Explanation:*** A number is classified as **even** or **odd** based on its divisibility by 2: **1. Even Number**: - A number is even…"
url: "https://vinothqaacademy.com/docs/basics-java-programs/"
updated: "2025-10-04"
category: Java Basic Programs
tags:
  - Java Basic Programs
---

# Basics Java Programs

**Frequently Asked Basics Java Programs**

## **Check if a Number is Even or Odd? - Program 1**

***Explanation:***

A number is classified as **even** or **odd** based on its divisibility by 2:

**1. Even Number**:

- A number is even if it is **divisible by 2** without leaving a remainder.

- Examples: 2, 4, 6, 8, 10, etc.

- Even numbers always end with 0, 2, 4, 6, or 8.

**2. Odd Number**:

- A number is odd if it is **not divisible by 2**, meaning it leaves a remainder of 1 when divided by 2.

- Examples: 1, 3, 5, 7, 9, etc.

- Odd numbers always end with 1, 3, 5, 7, or 9.

***In simple terms:***

- If a number can be split into two equal whole numbers, it's **even**.
- If it cannot, it's **odd**.

***Program:***

```javascript

                import java.util.Scanner;

public class EvenOddChecker {
    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.");
        }

        scanner.close();
    }
}

// Output
Enter a number: 7
7 is odd.

Enter a number: 12
12 is even.
```

## **Find the Factorial of a Number - Program 2**

***Explanation:***

The **factorial of a number** is the product of all positive integers from 1 to that number. It is denoted by the symbol **"!"**.

***For example:***

- **5! = 5 × 4 × 3 × 2 × 1 = 120**
- **3! = 3 × 2 × 1 = 6**

**Key points:**

1. The factorial of **0** is defined as **1** (0! = 1).
2. Factorials grow very quickly as the number increases.

***Program: ***

```javascript

                import java.util.Scanner;

public class FactorialCalculator {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

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

        int number = scanner.nextInt();

        long factorial = 1;

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

            factorial *= i;

        }

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

        scanner.close();

    }

}

// Output 

Enter a number: 5

Factorial of 5 is: 120

Enter a number: 0

Factorial of 0 is: 1
```

## **Find the Fibonacci Series** **- Program 3**

***Explanation : ***

The **Fibonacci Series** is a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. The sequence looks like this:

***Example : ***

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The first two numbers are fixed: **0** and **1**.

Every subsequent number is calculated by adding the two numbers before it.

***For example:***

- 0 + 1 = 1
- 1 + 1 = 2
- 1 + 2 = 3
- 2 + 3 = 5
- And so on.

This series is widely used in mathematics, computer science, and even nature (e.g., patterns in flower petals, pinecones, etc.).

***Program: ***

```javascript

                import java.util.Scanner;

public class FibonacciSeries {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

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

        int n = scanner.nextInt();

        int firstTerm = 0, secondTerm = 1;

        System.out.println("Fibonacci Series:");

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

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

            int nextTerm = firstTerm + secondTerm;

            firstTerm = secondTerm;

            secondTerm = nextTerm;

        }

        scanner.close();

    }

}

// Output:

Enter the number of terms: 5

Fibonacci Series:

0 1 1 2 3 

Enter the number of terms: 10

Fibonacci Series:

0 1 1 2 3 5 8 13 21 34
```

## ***Check if a Number is Prime* - Program 4**

***Explanation:***

A **prime number** is a number greater than 1 that has no positive divisors other than 1 and itself. In other words, it cannot be formed by multiplying two smaller natural numbers.

**Key Points:**

1. **Prime Numbers**: 2, 3, 5, 7, 11, 13, etc.
2. **Non-Prime Numbers (Composite)**: 4, 6, 8, 9, 10, etc.
3. **Special Case**: The number **1** is neither prime nor composite.

**How to Check:**

A number is prime if it is **not divisible by any number other than 1 and itself**.

**For example:**

- **7** is prime because it is only divisible by 1 and 7.
- **9** is not prime because it is divisible by 1, 3, and 9.

***Program:***

```javascript

                import java.util.Scanner;

public class PrimeNumberChecker {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

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

        int number = scanner.nextInt();

        boolean isPrime = true;

        if (number <= 1) {

            isPrime = false;

        } else {

            for (int i = 2; i <= Math.sqrt(number); i++) {

                if (number % i == 0) {

                    isPrime = false;

                    break;

                }

            }

        }

        if (isPrime) {

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

        } else {

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

        }

        scanner.close();

    }

}

// Output

Enter a number: 7

7 is a prime number.

Enter a number: 10

10 is not a prime number.

Enter a number: 1

1 is not a prime number.
```

*This series is widely used in mathematics, computer science, and even nature (e.g., patterns in flower petals, pinecones, etc.).*

## ***Reverse a Number*** **- Program 5**

***Explanation:***

Reversing a number means rearranging its digits in the opposite order.

**For example:**

- Original number: **1234**
- Reversed number: **4321**

***Program:***

```javascript

                import java.util.Scanner;

public class ReverseNumber {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

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

        int number = scanner.nextInt();

        int reversedNumber = 0;

        while (number != 0) {

            int digit = number % 10; // Extract the last digit

            reversedNumber = reversedNumber * 10 + digit; // Append the digit to the reversed number

            number = number / 10; // Remove the last digit

        }

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

        scanner.close();

    }

}

// Output

Enter a number: 1234

Reversed number: 4321

Enter a number: 56789

Reversed number: 98765
```

## **Check if a Number is Palindrome - Program 6**

***Explanation:***

A **palindrome number** is a number that remains the same when its digits are reversed. For example:

- **121** is a palindrome because reversing it gives **121**.
- **123** is not a palindrome because reversing it gives **321**.

***Program:***

```javascript

                import java.util.Scanner;

public class PalindromeChecker {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

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

        int number = scanner.nextInt();

        int originalNumber = number;

        int reversedNumber = 0;

        while (number != 0) {

            int digit = number % 10; // Extract the last digit

            reversedNumber = reversedNumber * 10 + digit; // Append the digit to the reversed number

            number = number / 10; // Remove the last digit

        }

        if (originalNumber == reversedNumber) {

            System.out.println(originalNumber + " is a palindrome.");

        } else {

            System.out.println(originalNumber + " is not a palindrome.");

        }

        scanner.close();

    }

}

// Output

Enter a number: 121

121 is a palindrome.

Enter a number: 123

123 is not a palindrome.

Enter a number: 1221

1221 is a palindrome.
```

## **Check if a Number is Armstrong - Program 7**

***Explanation:***

An **Armstrong number** (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example:

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXdtZIbUozVntPE_lKRJ9FXO0pfV6pZt87gsu-6h8Q6UASpfJZRagJBtJ41jyyzbwbm5ePdA0MFQ2904hWd_yTO8xoGLkTFKdRw4Qq2uOhatVtNthQSWnIU7CWgQvkqDiDhHVF0T?key=xWCWApjHZU15CKG_HeGKMp4G)

***Program:***

```javascript

                import java.util.Scanner;

public class ArmstrongNumberChecker {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

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

        int number = scanner.nextInt();

        int originalNumber = number;

        int sum = 0;

        int digits = String.valueOf(number).length(); // Count the number of digits

        while (number != 0) {

            int digit = number % 10; // Extract the last digit

            sum += Math.pow(digit, digits); // Add the digit raised to the power of digits

            number = number / 10; // Remove the last digit

        }

        if (originalNumber == sum) {

            System.out.println(originalNumber + " is an Armstrong number.");

        } else {

            System.out.println(originalNumber + " is not an Armstrong number.");

        }

        scanner.close();

    }

}

// Output

Enter a number: 153

153 is an Armstrong number.

Enter a number: 370

370 is an Armstrong number.

Enter a number: 123

123 is not an Armstrong number.
```

## **Calculate GCD and LCM of Two Numbers - Program 8**

***Explanation:***

1. **GCD (Greatest Common Divisor)**:

- The largest positive integer that divides both numbers without leaving a remainder.
- Example: GCD of 12 and 18 is **6**.

2. **LCM (Least Common Multiple)**:

- The smallest positive integer that is a multiple of both numbers.
- Example: LCM of 12 and 18 is **36**.

***Program***:

```javascript

                import java.util.Scanner;

public class GCDandLCM {

    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 gcd = calculateGCD(num1, num2);

        int lcm = (num1 * num2) / gcd; // Using the relationship between GCD and LCM

        System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd);

        System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm);

        scanner.close();

    }

    // Function to calculate GCD using Euclidean Algorithm

    public static int calculateGCD(int a, int b) {

        while (b != 0) {

            int temp = b;

            b = a % b;

            a = temp;

        }

        return a;

    }

}

// Output:

Enter the first number: 12

Enter the second number: 18

GCD of 12 and 18 is: 6

LCM of 12 and 18 is: 36

Enter the first number: 20

Enter the second number: 25

GCD of 20 and 25 is: 5

LCM of 20 and 25 is: 100
```

## **Swap two numbers using a temporary variable - Program 9**

***Explanation:***

Swapping two numbers means exchanging their values.

***For example:***

- Before swapping: a = 5, b = 10
- After swapping: a = 10, b = 5

***Program***: 

```javascript

                public class SwapNumbers {

    public static void main(String[] args) {

        int a = 5;

        int b = 10;

        System.out.println("Before swapping: a = " + a + ", b = " + b);

        // Swapping using a temporary variable

        int temp = a; // Store the value of a in temp

        a = b;       // Assign the value of b to a

        b = temp;    // Assign the original value of a (stored in temp) to b

        System.out.println("After swapping: a = " + a + ", b = " + b);

    }

}
```

## **Swapping Two Numbers without a Third Variable - Program 10**

***Program:***

```javascript

                import java.util.Scanner;

public class SwapNumbers {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

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

        int a = scanner.nextInt();

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

        int b = scanner.nextInt();

        System.out.println("Before swapping: a = " + a + ", b = " + b);

        // Swapping logic without a third variable

        a = a + b; // Step 1: a now holds the sum of a and b

        b = a - b; // Step 2: b now holds the original value of a

        a = a - b; // Step 3: a now holds the original value of b

        System.out.println("After swapping: a = " + a + ", b = " + b);

        scanner.close();

    }

}

// Output:

Enter the first number (a): 5

Enter the second number (b): 10

Before swapping: a = 5, b = 10

After swapping: a = 10, b = 5
```

***Alternative Way ***

a = a ^ b; // Step 1: a now holds the XOR of a and b

b = a ^ b; // Step 2: b now holds the original value of a

a = a ^ b; // Step 3: a now holds the original value of b

Note: This method is also efficient and avoids arithmetic overflow issue

## **Find the sum of digits of a number - Program 11**

***Explanation***: 

The **sum of digits of a number** refers to the total obtained by adding up each individual digit in the number. It is a basic mathematical operation that breaks down a number into its constituent digits and sums them together.

***Example*:**

Consider the number **1234**.

- The digits are: **1**, **2**, **3**, and **4**.
- The sum of the digits is: **1 + 2 + 3 + 4 = 10**.

***Program: ***

```javascript

                import java.util.Scanner;

public class SumOfDigits {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Input: Get the number from the user

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

        int number = scanner.nextInt();

        // Initialize sum to 0

        int sum = 0;

        // Process: Calculate the sum of digits

        int temp = number; // Temporary variable to hold the number

        while (temp != 0) {

            int digit = temp % 10; // Extract the last digit

            sum += digit;          // Add the digit to the sum

            temp = temp / 10;      // Remove the last digit

        }

        // Output: Display the sum of digits

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

        scanner.close();

    }

}

// output

Enter a number: 1234

Sum of digits of 1234 is: 10
```

## ***Find the largest of three numbers* - Program 12**

***Explanation: ***

The **largest of three numbers** refers to the greatest value among three given numbers. 

***Example**:*

Consider the numbers **12**, **25**, and **8**. Here the largest number is **25**.

***Program***:

```javascript

                import java.util.Scanner;

public class LargestOfThree {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Input: Get three numbers from the user

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

        int num1 = scanner.nextInt();

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

        int num2 = scanner.nextInt();

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

        int num3 = scanner.nextInt();

        // Logic: Find the largest number

        int largest;

        if (num1 >= num2 && num1 >= num3) {

            largest = num1; // num1 is the largest

        } else if (num2 >= num1 && num2 >= num3) {

            largest = num2; // num2 is the largest

        } else {

            largest = num3; // num3 is the largest

        }

        // Output: Display the largest number

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

        scanner.close();

    }

}

// Output

Enter the first number: 12

Enter the second number: 25

Enter the third number: 8

The largest number is: 25
```

## ***Find the smallest of three numbers*****- Program 13**

***Explanation: ***

The **smallest of three numbers** refers to the smallest value among three given numbers. 

***Example*:**

Consider the numbers **12**, **25**, and **8**. Here the smallest number is **8**.

***Program*:**

```javascript

                import java.util.Scanner;

public class SmallestOfThree {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Input: Get three numbers from the user

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

        int num1 = scanner.nextInt();

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

        int num2 = scanner.nextInt();

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

        int num3 = scanner.nextInt();

        // Logic: Find the smallest number

        int smallest;

        if (num1 <= num2 && num1 <= num3) {

            smallest = num1; // num1 is the smallest

        } else if (num2 <= num1 && num2 <= num3) {

            smallest = num2; // num2 is the smallest

        } else {

            smallest = num3; // num3 is the smallest

        }

        // Output: Display the smallest number

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

        scanner.close();

    }

}
// Output

Enter the first number: 12

Enter the second number: 25

Enter the third number: 8

The smallest number is: 8
```

## ***Check if a number is a perfect number*****- Program 14**

***Explanation: ***

A **perfect number** is a positive integer that is equal to the sum of its **proper divisors** (excluding itself). In other words, if you add up all the numbers that divide the number evenly (except the number itself), the sum should be equal to the number.

*Example:*

- **6** is a perfect number because its proper divisors are **1, 2, and 3**, and **1 + 2 + 3 = 6**.
- **28** is also a perfect number because its proper divisors are **1, 2, 4, 7, and 14**, and **1 + 2 + 4 + 7 + 14 = 28**.

***Program: ***

```javascript

                import java.util.Scanner;

public class PerfectNumber {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Input: Get the number from the user

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

        int number = scanner.nextInt();

        // Logic: Check if the number is perfect

        int sum = 0;

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

            if (number % i == 0) {

                sum += i; // Add proper divisors to the sum

            }

        }

        // Output: Display the result

        if (sum == number) {

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

        } else {

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

        }

        scanner.close();

    }

}

// Output

Enter a number: 28

28 is a perfect number.

Enter a number: 12

12 is not a perfect number.
```

*** ***

## ***Find the square root of a number*****- Program 15**

***Explanation:***

**Square Root of a Number**

The **square root of a number** is a value that, when multiplied by itself, gives the original number. It is denoted by the symbol **√**.

**Example:**

- The square root of **25** is **5** because **5 × 5 = 25**.
- The square root of **9** is **3** because **3 × 3 = 9**.

***Program:***

```javascript

                import java.util.Scanner;

public class SquareRoot {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Input: Get the number from the user

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

        double number = scanner.nextDouble();

        // Logic: Calculate the square root

        double squareRoot = Math.sqrt(number);

        // Output: Display the square root

        System.out.println("The square root of " + number + " is: " + squareRoot);

        scanner.close();

    }

}

// Output

Enter a number: 25

The square root of 25.0 is: 5.0
```

## **Find whether leap year or not** - **Program 16**

***Explanation***: 

**Leap Year**

A **leap year** is a year that has **366 days** instead of the usual **365 days**. The extra day is added to the month of **February**, making it **29 days long** instead of 28. Leap years are introduced to keep the calendar year synchronized with the **astronomical year** (the time it takes Earth to orbit the Sun).

***Program:***

```javascript

                import java.util.Scanner;

public class LeapYear {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Input: Get the year from the user

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

        int year = scanner.nextInt();

        // Logic: Check if the year is a leap year

        boolean isLeapYear = false;

        if (year % 4 == 0) {

            if (year % 100 == 0) {

                if (year % 400 == 0) {

                    isLeapYear = true; // Divisible by 400

                }

            } else {

                isLeapYear = true; // Divisible by 4 but not by 100

            }

        }

        // Output: Display the result

        if (isLeapYear) {

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

        } else {

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

        }

        scanner.close();

    }

}

// Output:

Enter a year: 2024

2024 is a leap year.

Enter a year: 1900

1900 is not a leap year.
```

## **Convert a decimal number to binary** - **Program 17**

***Explanation:***

Converting a decimal number to binary involves representing the number in base 2 (using only 0s and 1s). The process works as follows:

1. **Divide the decimal number by 2**.
2. **Record the remainder** (either 0 or 1).
3. **Update the number** to be the quotient of the division.
4. **Repeat the process** until the number becomes 0.
5. The binary number is the sequence of remainders read in reverse order.

**Example:**

Convert **13** to binary:

- 13 ÷ 2 = 6, remainder **1**
- 6 ÷ 2 = 3, remainder **0**
- 3 ÷ 2 = 1, remainder **1**
- 1 ÷ 2 = 0, remainder **1**
- Binary: **1101** (read remainders in reverse order).

***Program: ***

```javascript

                import java.util.Scanner;

public class DecimalToBinary {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

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

        int decimal = scanner.nextInt();

        StringBuilder binary = new StringBuilder();

        if (decimal == 0) {

            binary.append("0"); // Handle the special case for 0

        } else {

            while (decimal > 0) {

                int remainder = decimal % 2; // Get the remainder

                binary.insert(0, remainder); // Insert the remainder at the beginning

                decimal = decimal / 2; // Update the number

            }

        }

        System.out.println("Binary representation: " + binary.toString());

        scanner.close();

    }

}

// Output

Enter a decimal number: 13

Binary representation: 1101

Enter a decimal number: 25

Binary representation: 11001

Enter a decimal number: 0

Binary representation: 0
```

## **Convert a binary number to decimal** - **Program 18**

***Explanation:***

Converting a binary number to decimal involves calculating the decimal equivalent of a number represented in base 2 (using 0s and 1s). 

1. Start from the **rightmost digit** (least significant bit).
2. Multiply each binary digit by 2 power n, where *n* is its position (starting from 0).
3. Sum all the results to get the decimal number.

Example: 

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXdUpW3OrAbg2vsQKK8tKJOPRChbrhj1BiMNMzMkwXIDiBZq7tzWvW8ZfD6_URiUnfbBc1oMScUOSOIx22mjQhrUrK2Cl2QjzuIutK1K-nPP6P86rdbYOW4wCMG3aLjZhT9-LmQfKA?key=xWCWApjHZU15CKG_HeGKMp4G)

***Program***: 

```javascript

                import java.util.Scanner;

public class BinaryToDecimal {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

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

        String binary = scanner.nextLine();

        int decimal = 0;

        int power = 0;

        // Start from the end of the string (rightmost digit)

        for (int i = binary.length() - 1; i >= 0; i--) {

            char ch = binary.charAt(i);

            if (ch == '1') {

                decimal += Math.pow(2, power); // Add 2^power to the result

            }

            power++; // Increment the power for the next digit

        }

        System.out.println("Decimal representation: " + decimal);

        scanner.close();

    }

}

// Output

Enter a binary number: 1101

Decimal representation: 13

Enter a binary number: 1010

Decimal representation: 10

Enter a binary number: 1111

Decimal representation: 15
```

## **Find the Sum of multiples of 3** - **Program 19**

***Explanation:***

The sum of multiples of 3 involves adding all numbers within a given range that are divisible by 3. 

For example:

- Range: 1 to 10
- Multiples of 3: 3, 6, 9
- Sum:  3+6+9=18

***Program:***

```javascript

                import java.util.Scanner;

public class SumOfMultiplesOf3 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the upper limit (n): ");

        int n = scanner.nextInt();

        int sum = 0;

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

            if (i % 3 == 0) { // Check if the number is divisible by 3

                sum += i; // Add the number to the sum

            }

        }

        System.out.println("Sum of multiples of 3 from 1 to " + n + " is: " + sum);

        scanner.close();

    }

}

// Output

Enter the upper limit (n): 10

Sum of multiples of 3 from 1 to 10 is: 18

Enter the upper limit (n): 20

Sum of multiples of 3 from 1 to 20 is: 63
```

## **Find the Sum of prime digits** - **Program 20**

***Explanation***:

The sum of prime digits involves identifying the prime digits in a number and calculating their sum. Prime digits are digits that are prime numbers. The prime digits are **2, 3, 5, and 7**.

***Program:***

```javascript

                import java.util.Scanner;

public class SumOfPrimeDigits {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

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

        int number = scanner.nextInt();

        int sum = 0;

        while (number > 0) {

            int digit = number % 10; // Extract the last digit

            if (isPrimeDigit(digit)) { // Check if the digit is prime

                sum += digit; // Add the prime digit to the sum

            }

            number = number / 10; // Remove the last digit

        }

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

        scanner.close();

    }

    // Function to check if a digit is prime

    public static boolean isPrimeDigit(int digit) {

        return digit == 2 || digit == 3 || digit == 5 || digit == 7;

    }

}

// Output

Enter a number: 12345

Sum of prime digits: 10

Enter a number: 7352

Sum of prime digits: 17
```

## **Find the sum of the series 1 + 2 + 3 + ... + n** - **Program 21**

***Explanation:***

The sum of the series 1+2+3+⋯+n  is the total of all integers from 1 to  n.

This is a simple arithmetic series where each term increases by 1.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXfEL2mw3cP4W3MroN9aqN7nbqrmCTVnlYyFuDHl_t4ZgxGfUOcq9Yo0P2TG5EBhZyOzTNtvIdkUDhyvC08pvE4fVAyWHtfReGH3EypgMN4KUna-G8FWZnZnm27m0ZrXTFcE56R-?key=xWCWApjHZU15CKG_HeGKMp4G)

***Program:***

```javascript

                import java.util.Scanner;

public class SumOfSeries {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the value of n: ");

        int n = scanner.nextInt();

        int sum = n * (n + 1) / 2; // Using the formula

        System.out.println("Sum of the series 1 + 2 + 3 + ... + " + n + " is: " + sum);

        scanner.close();

    }

}

// output:

Enter the value of n: 5

Sum of the series 1 + 2 + 3 + ... + 5 is: 15

Enter the value of n: 10

Sum of the series 1 + 2 + 3 + ... + 10 is: 55
```
