Computer Science, asked by Anonymous, 1 year ago

Write A Java Program to input 5 numbers and then find and print the smallest perfect number.​

Answers

Answered by Anonymous
8

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);

   }

}

         

EXPLANATION :

⏩ The perfect number is a number whose sum of factors is equal to the number . For example : 6 = 1 + 2 + 3 .

⏩ We store the given numbers in an array for our benefit .

⏩ Then we will check whether the number is perfect or not .

⏩ If it is perfect then store it in min provided that min = 0 .

⏩ A flag variable is run so that the first perfect number can be trapped .

⏩ Then each time we will check whether the given number is smaller than the previous perfect number .

⏩ If there is no perfect number , there will be no output at all !


Anonymous: ..Very Very Thanks For Ur Answer.....!....I needed that The Most
Anonymous: welcome :)
bbbb67: Fab answer sir
bbbb67: Inbox sir
bbbb67: It's urgent
Anonymous: Ok
Answered by Anonymous
0

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);

   }

}

Similar questions