Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Code Correction!!! Calculate GCD of given two input integers. Match the exa

ID: 672629 • Letter: J

Question

Java Code Correction!!!

Calculate GCD of given two input integers. Match the example outputs:

Sample Run 1

Enter two Integers

72

18

gcd(72,18)= gcd(72-18,18)=gcd(54,18)

gcd(54,18)= gcd(54-18,18)=gcd(36,18)

gcd(36,18)= gcd(36-18,18)=gcd(18,18)

gcd(18,18)= gcd(18, 18- 18)=gcd(18,0)

gcd(18,0)=18

gcd(72,18)=18

Sample Run 2:

Enter two Integer

13

101

gcd(13,101)= gcd(13, 101- 13)=gcd(13,88)

gcd(13,88)= gcd(13, 88- 13)=gcd(13,75)

gcd(13,75)= gcd(13, 75- 13)=gcd(13,62)

gcd(13,62)= gcd(13, 62- 13)=gcd(13,49)

gcd(13,49)= gcd(13, 49- 13)=gcd(13,36)

gcd(13,36)= gcd(13, 36- 13)=gcd(13,23)

gcd(13,23)= gcd(13, 23- 13)=gcd(13,10)

gcd(13,10)= gcd(13-10,10)=gcd(3,10)

gcd(3,10)= gcd(3, 10- 3)=gcd(3,7)

gcd(3,7)= gcd(3, 7- 3)=gcd(3,4)

gcd(3,4)= gcd(3, 4- 3)=gcd(3,1)

gcd(3,1)= gcd(3-1,1)=gcd(2,1)

gcd(2,1)= gcd(2-1,1)=gcd(1,1)

gcd(1,1)= gcd(1, 1- 1)=gcd(1,0)

gcd(1,0)=1

gcd(13,101)=1

Please check my code and change what I do not have right to match the output.

THANK YOU!

import java.util.Scanner;

public class Lab6

{

   public static void main(String[] args)

   {

   int x, y;

  

   Scanner scan = new Scanner(System.in);

  

   System.out.println ("Enter two integers (space between integers): ");

   x= scan.nextInt();

   y= scan.nextInt();

  

int tempx=x;

int tempy=y;

  

   if (x>y)

   {

       x=x-y;

       System.out.println("GCD (" + tempx + " , " + tempy + ")= GCD (" + tempx + " - " + tempy + " , " + tempy + " )= GCD (" + x + " , " + tempy + ")" );

   }

else

   {

       y=y-x;

       System.out.println("GCD (" + tempx + " , " + tempy + ")= GCD (" + tempx + " , " + tempy + " - " + tempx + " )= GCD (" + tempx + " , " + y + ")" );

   }

  

   while (x!=0 && y!=0)

   {

      

       if (x>y)

       {

           x=x-y;

           System.out.println("GCD (" + x + " , " + tempy + ")= GCD (" + x + " - " + tempy + " , " + tempy + " )= GCD (" + x + " , " + tempy + ")" );

       }

      

      

       else

       {

           y=y-x;

           System.out.println("GCD (" + tempx + " , " + y + ")= GCD (" + tempx + " , " + y + " - " + tempx + " )= GCD (" + tempx + " , " + y + ")" );

       }

       }

  

   if (x==0)

   {

       System.out.print("GCD (" + tempx + " , " + tempy + ")= " + y);

   }

else

   {

       System.out.print("GCD (" + tempx + " , " + tempy + ")= " + x);

   }

   {

      

   }

  

  

}

}

Explanation / Answer

import java.util.Scanner;

public class Lab6

{

    public static void main(String[] args)

    {

    int x, y;

  

    Scanner scan = new Scanner(System.in);

  

    System.out.println ("Enter two integers (space between integers): ");

    x= scan.nextInt();

    y= scan.nextInt();

/* I have added this two variables which store initial values input by user. Because tempx,tempy values got change during execution. */  
int finalx=x;
int finaly=y;

int tempx=x;

int tempy=y;

  

    if (x>y)

    {

        x=x-y;

        System.out.println("GCD (" + tempx + " , " + tempy + ")= GCD (" + tempx + " - " + tempy + " , " + tempy + " )= GCD (" + x + " , " + tempy + ")" );
        /* I have added this below line tempx=x. Because x contain the new value and if we use X directly then first line in while loop again changing value of x so in that case we will not able to use old value of X. */
        tempx=x;
    }

else

    {

        y=y-x;

        System.out.println("GCD (" + tempx + " , " + tempy + ")= GCD (" + tempx + " , " + tempy + " - " + tempx + " )= GCD (" + tempx + " , " + y + ")" );
        /* Similarly I have added this below line tempy=y. Because Y contain the new value and if we use Y directly then first line in while loop again changing value of Y so in that case we will not able to use old value of Y. */
   
        tempy=y;
    }

  

    while (x!=0 && y!=0)

    {

      

        if (x>y)

        {

            x=x-y;

            System.out.println("GCD (" + tempx + " , " + tempy + ")= GCD (" + tempx + " - " + tempy + " , " + tempy + " )= GCD (" + x + " , " + tempy + ")" );
    /* For Similar reasons as above mentioned I have added this below line tempx=x. Because x contain the new value and if we use X directly then first line in while loop again changing value of x so in that case we will not able to use old value of X. */

            tempx=x;
        }

      

      

        else

        {

            y=y-x;

            System.out.println("GCD (" + x + " , " + tempy + ")= GCD (" + tempx + " , " + tempy + " - " + tempx + " )= GCD (" + tempx + " , " + y + ")" );
/* For Similar reasons as above mentioned I have added this below line tempy=y. Because Y contain the new value and if we use Y directly then first line in while loop again changing value of Y so in that case we will not able to use old value of Y. */

            tempy=y;
        }

        }

  

    if (x==0)

    {

        System.out.println("GCD (" + tempx + " , " + tempy + ")= " + y);
/*This is the reason I have declared these two variables above to print line of output. */    
        System.out.print("GCD (" +finalx + " , " + finaly + ")= " + y);

    }

else

    {

        System.out.println("GCD (" + tempx + " , " + tempy + ")= " + x);
    /*Similar reason as above I have declared these two variables above to print line of output. */    

        System.out.print("GCD (" +finalx + " , " + finaly + ")= " + x);

    }

  
  

  

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote