GFG Mathematics 2
package com.company;
import java.util.Scanner;
public class CWR_Mathematics_Gfg02 {
// gcd 3
public static int gcd3(int a, int b){
if(b == 0){
return a;
}
else
return gcd3(b,a%b);
}
// gcd m2
public static int gcd2(int a , int b){
while(a!=b){
if(a>b){
a = a-b;
}
else b = b-a;
}
return b;
}
// Naive method for gcd
public static int gcd (int a,int b){
int res = Math.max(a,b);
while(res>0){
if(a%res ==0 && b%res==0){
break;
}
res--;
}
return res;
}
public static int Gcd(int a ,int b){
int c = 0;
if(a<b && a%b!=0 && b%a!=0){
while(b%a!=0){
c = b%a;
b = a;
a = c;
}
return c;
}
else if(a>b && a%b!=0 && b%a!=0){
while(a%b!=0){
c = a%b;
a = b;
b = c;
}
}
else if (a%b == 0){
return b;
}
else{
return a;
}
return c;
}
public static int factorial(int n){
int fact = 1;
for(int i=1; i<=n;i++){
fact = fact*n;
}
return fact;
}
public static long findZeroes(int x){
int res = 0;
for (int i=5; i<=x ; i=i*5){
res = res + x/i;
}
return res;
}
public static void main(String[] args) {
System.out.println("Enter the number");
Scanner sc = new Scanner(System.in);
System.out.println(gcd3(sc.nextInt(),sc.nextInt()));
}
}
Comments
Post a Comment