perfect number || deficit number || abundant number || Java programs || Java

In this, we are going to write A program in Java to input an integer and check whether it is perfect, abundant or deficient number. If the sum of the factors excluding itself is equal to that number it is perfect, if greater than that number it is abundant and if less than that number it is deficient number.


import java.util.*;

class Perfect 
{
    public static void main(String[] args) 
    {
        int i, n, s = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        n = sc.nextInt();
        for (i = 1; i < n; i++) 
        {
            if (n % i == 0) 
            {
                s += i;
            }
        }
        if (s == n) 
        {
            System.out.println("Perfect Number");
        } 
        else if (s > n) 
        {
            System.out.println("Abundant Number");
        } 
        else 
        {
            System.out.println("Deficient Number");
        }
    }
}
 

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post