Java program to input 5 numbers and print only perfect numbers.
Using Scanner
Answers
Answered by
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]);
}
}
}
}
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
Similar questions