Number

This Java class, Number_Questions, prompts the user to input a number and then computes the sum of its digits. Additionally, it provides methods to check if a number is a palindrome, whether it's prime, and to calculate its factorial.
 avatar
unknown
java
a year ago
1.2 kB
5
Indexable
import java.util.*;

public class Number_Questions {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the Number : ");
        int num = sc.nextInt();
        System.out.println("Sum of Digits of " + num + " = " + SumOfDigits(num));
        sc.close();
    }

    public static boolean PalindromeCheck(int x) {
        int orig = x;
        int neww = 0;
        while (x > 0) {
            neww = (10 * neww) + (x % 10);
            x /= 10;
        }
        if (orig == neww)
            return true;
        else
            return false;
    }

    public static boolean PrimeCheck(int x) {
        for (int i = 2; i <= Math.sqrt(x); i++) {
            if (x % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static int CalculateFactorial(int x) {
        if (x == 0)
            return 1;
        int product = x * CalculateFactorial(x - 1);
        return product;
    }

    public static int SumOfDigits(int x) {
        int sum = 0;
        while (x > 0) {
            sum += (x % 10);
            x /= 10;
        }
        return sum;
    }
}
Editor is loading...
Leave a Comment