Perfect number in java - Write a program to find perfect number or not

 

Perfect number in java

Perfect number in java

A perfect number is a positive number (integer number) is the sum total of its factor except for the number itself. For example 6, has factor 1, 2, 3 (except 6) and add it all 1 + 2 + 3 = 6. Here summation of its divisors (excluding the number) is equal to the original number.

In the following code a number is taken by the user in n = sc.nextInt() and then with With the help of for loop, we do a summation of its divisor, less them the number. After then with the help of if condition statement

        if(s == n)

         System.out.println("Perfect Number");

        else

         System.out.println("Not a perfect Number");      

We check the number is equal to the calculated summation in s. if both are equal then the number is the perfect number.

 

import java.util.*;

public class PerfectNumber

{

    public static void main(String args[])

    {

        int i, s=0, n;

        Scanner sc=new Scanner(System.in);

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

        n=sc.nextInt();

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

        {

            if(n % i == 0)

                s = s + i;

        }

        if(s == n)

         System.out.println("Perfect Number");

        else

         System.out.println("Not a perfect Number");      

    }

}

Perfect number in java with the method.

The below program is done with the help of a method

int perfectNumber(int num)

    {

        sum = 0;

        number = num;

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

        {

            if(number % i == 0)

                sum =sum + i;

        }

        return sum;

    }

In the above piece of code we do the same calculation as above but within a block called perfectNumber(int num) where the supplied number is sum up of its factor (except the number itself)  and return to the value. Then in the main() method, it checked the original number and if it is equal to the return value then that number is a perfect number.

import java.util.*;

public class PerfectNumberMethod

{

    private int i;      // for iteraion

    private int sum;    // for summation of factor

    private int number; // number to check perfect number

    int perfectNumber(int num)

    {

        sum = 0;

        number = num;

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

        {

            if(number % i == 0)

                sum =sum + i;

        }

        return sum;

    } // end of perfect method

    // main method started

    public static void main(String args[])

    {

        int i, s, n;

        PerfectNumberMethod ob = new PerfectNumberMethod();

        Scanner sc=new Scanner(System.in);

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

        N = sc.nextInt();

        s = ob.perfectNumber (n);

        if(s == n)

            System.out.println ("Perfect Number");

        else

            System.out.println ("Not a perfect Number");      

    }

}

Perfect number in java using recursion

import java.util.*;

public class PerfectNumberWithRecursion

{

    int sum, number, i;   

    PerfectNumberWithRecursion()

    {

        sum = 0;

        i = 1;

    }

 

    int perfectNumber(int num)

    {

        number = num;

        if(i<=number/2)

        {

            if(number%i==0)

            {

                sum =sum + i;

            }

            i++;

            perfectNumber(number);

        }

        return sum;   

    }

    // main method started

    public static void main(String arg[])  

    {

        int n, s;

        Scanner sc=new Scanner(System.in);      

        System.out.println("Enter a number");

        n=sc.nextInt();

        PerfectNumberWithRecursion ob=new PerfectNumberWithRecursion( );

        s = ob.perfectNumber(n);

        if(s == n)

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

        else

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

    }

}

More Java program


SHARE THIS
Previous Post
Next Post