gcd for n numbers using euclid as logic
import java.util.Scanner;
class Main
{
public static int gcd(int A,int B)
{
int a =A;
int b=B;
int a_,b_;
while(a!=0)
{
a_=b%a;
b_=a;
a=a_;
b=b_;
}
return b;
}
public static void multipleInputs()
{
Scanner sc= new Scanner(System.in);
System.out.println("enter number of inputs");
int N=sc.nextInt();
System.out.println("start enterning your"+N+"inputs");
int ans=sc.nextInt();
for(int i =1 ;i<=N-1;i++)
{
int no=sc.nextInt();
ans=gcd(ans,no);
}
System.out.println(ans);
}
public static void main(String args[])
{
multipleInputs();
}
}
i/p:
5
10 20 40 12 6
[Success] Your code was executed successfully
enter number of inputs
start enterning your5inputs
2