HCF of multiple number.

WAP to find HCF of multiple number.

import java.util.*;

class L19HCF

{

    public static void main()

    {

        int x,i=0;

        System.out.println("Enter the number of integers to calculate their Highest Common Factor");

        Scanner in = new Scanner(System.in);

        x = in.nextInt();

        int [] a=new int[x];

        for(i=0;i<x;i++)

        {

            System.out.print(" \n Enter Integer  "+(i+1) + "\t");

            a[i]=in.nextInt(); // store in value input by user in array

        }

        Arrays.sort(a);

        for(i=0;i<(x-1);i++)

        {

            while(a[i+1]%a[i]!=0)

            {

                int temp=a[i];

                a[i]= a[i+1] % a[i];

                a[i+1]=temp;

            }

            a[i+1]=a[i];

        }

        System.out.println("\n Highest Common Factor of given integers is  "+a[i]);

    }

}

Comments

Popular Posts