Program to find greatest common divisior of two numbers using function in java.
import java.util.*;
public class function {
public static int greatestCommonDivisior(int a,int b){
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return b;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter two numbers=");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.print("the greatest common divisior of two numbers is= "+greatestCommonDivisior(a,b));
}
}
■》Output->
enter two numbers=>
30
10
the greatest common divisior of two numbers is= 10
Comments
Post a Comment