Write a Java Program to input 5 numbers and then find and print the smallest perfect number without using array.
Answers
Explanation:
CODE :
import java.util.*;
class smallest_perfect_number
{
public void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[5] ;
System.out.println("Enter 5 number");
for( int i=0;i<5;i++ )
a[i] =sc.nextInt();
int s=0;
int f=0;
int min=0;
for(int i=0;i<5;i++)
{
for(int j=1;j<=a[i];j++)
{
if(n%j==0)
{
s=s+j;
}
}
if(s==a[j])
{
++f;
if(f==1)
min=a[j];
if(min>a[j])
min=a[j];
}
}
System.out.println("Smallest perfect number = "+min);
}
}
Answer:
*CODE*
import java.util.*;
class smallest_perfect_number
{
public void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[5] ;
System.out.println("Enter 5 number");
for( int i=0;i<5;i++ )
a[i] =sc.nextInt();
int s=0;
int f=0;
int min=0;
for(int i=0;i<5;i++)
{
for(int j=1;j<=a[i];j++)
{
if(n%j==0)
{
s=s+j;
}
}
if(s==a[j])
{
++f;
if(f==1)
min=a[j];
if(min>a[j])
min=a[j];
}
}
System.out.println("Smallest perfect number = "+min);
}
}