Amicable Pair Number

 //Wap to accept two numbers and check it is amicable numbers or not.

//the sum of the factor of the first number is equal to the second number and the sum of the factor of

//the second number is equal to the first number, then it is an amicable pair.

//ex- (220,284) are amicable pair

//sum of factors 220=(1,2,4,5,10,11,20,22,44,55,110)=284

//sum of factors 284=(1,2,4,71,142)=220

import java.util.*;

class amicable

{//class amicable opens

    int a=0,b=0;

    void accept()//method to accept the nos

    {

        Scanner sc=new Scanner(System.in);

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

        a=sc.nextInt();//220

        System.out.println("Enter another no");

        b=sc.nextInt();//284

    }

    int sumfactor(int n)//method to return the sum of the factors

    {

        int sum=0;

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

        {

            if(n%i==0)

            sum=sum+i;

        }

        return sum;

    }

    void display()//method to display the results

    {

        if((sumfactor(a)==b)&&(sumfactor(b)==a))//284==284 true && 220==220 true

        System.out.println(a+" and "+b+" is an amicable pairs");

        else

        System.out.println("Not an amicable pairs");

    }

    public static void main(String[]args)

    {//main method opens

        amicable obj=new amicable();

        obj.accept();

        obj.display();

    }//main method closes

}//class closes


No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...