Friday, December 27, 2013

greatest common divisor Java program



//finding gcd
class gcd
{    public static void main(String args[])
     {   int n1,n2,sn,i;
         n1=Integer.parseInt(args[0]);
         n2=Integer.parseInt(args[1]);
         if ( n1 < n2 )
            sn = n1;
         else
            sn = n2;
         for ( i=sn ; i>=1;i --)
            if( n1%i==0 && n2 %i==0) break;
         if(i==1)
            System.out.println("No GCD");
         else
            System.out.println("GCD is " + i);
     }
}

Compile တဲ့အခါ မွာ
javac gcd.java


Run တဲ့အခါ မွာ
java gcd 24 36

အဲဒီအခါမွာ 24 36 ရဲ့  Greatest common divisor (gcd) (အႀကီးဆုံးဘုံဆခြဲကိန္း = ကိန္းႏွစ္လုံးကို စားျပတ္တဲ့ ကိန္းထဲကအႀကီးဆုံး) ကို ရွာၿပီး 12 ကို ျပန္ရိုက္ျပလိမ့္မည္။

No comments:

Post a Comment