Computer Science, asked by Varun1870, 1 year ago

Java program to input 5 numbers and print only perfect numbers.
Using Scanner

Answers

Answered by QGP
8
import java.util.Scanner;
public class PerfectNumber
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        System.out.println("A perfect number is a number that is equal to the Sum of its proper divisors except the number itself\n");

        int num[] = new int[5];

        for(int i=0;i<5;i++)        //Loop for accepting input
        {
            System.out.print("Enter Number: ");
            num[i] = sc.nextInt();
        }

        System.out.println("\nThe Perfect Numbers are: ");

        for(int i=0;i<5;i++)
        {
            int sum=0;
            for(int j=1; j<=(num[i]/2);j++)     //For each number, we first find proper divisors
            {
                if(num[i]%j==0)         //If number is divisble by i, then i is a divisor
                {
                    sum += j;       //We add i to the "sum" of proper divisors
                }
            }

            if (sum==num[i])
            {
                System.out.println(num[i]);
            }
            
        }

    }
}


raji22: mmm i asked
raji22: in previous year end
raji22: hi
QGP: Dear Users, Please Stop Chatting in Comments Section. This place is meant for queries and comments related to Question only.
Similar questions