Starting with Algorithm

Hello world, this is the new starting of mrpsyclone ,here you can post your problems about computer science that may be related to algorithm, database, computer security or any uselfull links related to computer science ,since mrpsyclone is dedicated to computer science.
This is the first post about the algorithm, when I open the book of algorithm I found the first question that is GCD the GCD that is Greatest common divisor for example THE GCD of 17 and 34 is 17 since the greatest common divisor is 17 since 17=17*1; and 34=17*2; the common greatest common number is 171
 Here I post a program about it it is quite simple to understand but this algorithm is not correct since it is giving invalid result.if you think you can make it correct ,then you can post it in comments,this is the little  start to exploring the world of Computer science .

package fabonacci_series;
import java.util.Scanner;
public class GCD {
//public void  GCD(int,int );
       public static void main(String[] args) {
             Scanner sc=new Scanner(System.in);
             System.out.print("Enter the numbers");
             int a,b;
             a=sc.nextInt();
             b=sc.nextInt();
             GCD(a,b);
       }

       public static void  GCD(int q,int r)
       {
int temp;
if(q>r)
{      temp=q/r;
q=q/temp;
}

else
{      temp=r/q;
q=r/temp;
       }
System.out.print("GCD is "+q);
       }
      
            
      
}


Comments

Popular Posts