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

Been working on this program for hours and keep getting error please help and sh

ID: 672772 • Letter: B

Question

Been working on this program for hours and keep getting error please help and show a working output, Thank you

Step 2: Declaring variables

Examining the problem we need to have 2 variable to store integer input given by the user.

    int x,y;

Step 3: setting up Scanner object and scanning the inputs

import java.util.Scanner;

Create Scanner class object by using following syntax

Scanner=new Scanner(System.in); /* give some name to the

object which ever you want */

Ask the user to input the integer values for whom the GCD is to be calculated

System.out.println ( “Enter two integers “);

With the scanner class object stores the values given by the use in x and y variables.

Step4: Getting started with the logic

To calculate the GCD of the two numbers, we need to subtract the given two numbers until one

number becomes 0.

This should be done iteratively, meaning we need to use loops to implement the logic.

Step5: Get Started with the loop

Before we start the loop we need to declare two variable tempx, tempy which are integers to hold

the initial value of x and y inputs.

This is because we do manipulations on x and y and at each iteration x and y hold different

values

  int tempx=x;

int tempy=y;

Start the while loop

while ()

Remember every loop should have a termination condition. Here the condition would be x!=0

&& y!=0 (both should be not equal to 0 or else it might result infinite loop )

Step6: Arithmetic Operations inside the loop

After initializing the loop we need to subtract x from y if(y > x) or y from x (if x>y) and find

GCD of the resultant x and y   

inside the loop check which integer is greater and subtract other integer from this one

for example

if( x>y)

{

    x=x-y;

  }

else

  {

    y=y-x;

  }

Now perform the same operation on new x and y variables until one of the variable becomes 0

Print the intermediate values of the x and y variables so that the user will understand how the

GCD is calculated.

Step7: After the end of loop

At the end of the loop either x or y will be non zero integer.The one which is non zero integer is

the GCD of the given input numbers

Remember we have the initial values of x and y variables stored in tempx and tempy

Print on the console the initial values of input using tempx and tempy

System.out.print(“The GCD of the input value (“ + tempx + tempy

+ “)= “);

then print either x or y depending which is non-zero integer

Sample Output

Below is an example of what your output should roughl

All text in bold represents user input.

Sample Run 1

Enter two Integer

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

HERE IS THE OUTLINE FOR THE PROGRAM

import java.util.Scanner; /* importing Scanner class */
public class HW6 {
public static void main(String[] args)
        {
            /* creating scanner class object */
            System.out.println("Enter two Integer"); /* asking the user to enter two integers */
            int x,y;
            /* scanner the intergers her */
           
           
            int tempx=x; /* storing the initial value of x*/
            int tempy=y; /* storing the initial value of y */
           
            while((x!=0)&& (y!=0)) /* start of while loop */
            {
                System.out.print("gcd(" +x+ "," +y + ")="); /* printing on console */
                    if(<condition>)
                    {
`                        /* show the user how the value will be changing (check sample output to print the statement )*/
                        /*store the new value of the variable */
                    }
                    else
                    {
                        /* show the user how the value will be changing (check sample output to print the statement )*/
                        /*store the new value of teh variable */
                    }
                    System.out.println("gcd(" +x +"," +y +")");
                   
            }
            if (<condition>) /* check which variable is 0 */
            {
                /* print gcd value */
                /* print the final statements gcd (x,y) = <ans> */
            }
            else
            {
                /* print gcd value */
                /* print the final statements gcd (x,y) = <ans> */
            }
           
           
        /* closing scanner class object */
        }
}

Explanation / Answer

HW6.java

/*

Expert Answers By CHEGG INDIA

*/

/**

*

* @author Chegg India

*/

import java.util.Scanner; /* importing Scanner class */

public class HW6 {

public static void main(String[] args)

        {

            /* creating scanner class object */

            System.out.println("Enter two Integer"); /* asking the user to enter two integers */

            int x,y;

            /* scanner the intergers her */

            Scanner sc=new Scanner(System.in);

            System.out.println("enter x value");

            x=sc.nextInt();

            System.out.println("The value of x is"+x);

            System.out.println("enter y value");

            y=sc.nextInt();

            System.out.println("The value of y is"+y);

            int tempx=x; /* storing the initial value of x*/

            int tempy=y; /* storing the initial value of y */

            while((x!=0)&& (y!=0)) /* start of while loop */

            {

                System.out.print("gcd(" +x+ "," +y + ")="); /* printing on console */

                    if(x<y)

                    {

/* show the user how the value will be changing (check sample output to print the statement )*/

                        /*store the new value of the variable */

                        y=y-x;

                    }

                    else

                    {

                        /* show the user how the value will be changing (check sample output to print the statement )*/

                        /*store the new value of teh variable */

                    x=x-y;

                    }

                if(x==y) //if x=y then update y=y-x

                {

                    y=x-y;

                }

            }

                           

            if (y==0) /* check which variable is 0 */

            {

                //if y=0 then gcd of x,y is x

                System.out.println("gcd of x,y is "+x);

            }

            else

            {

                y=x-y;

            //if y not equal to 0 update y=x-y

            }

      

           

        /* closing scanner class object */

        }

}

Output

Test case1:

Enter two Integer

enter x value

72

The value of x is72

enter y value

18

The value of y is18

gcd(72,18)=gcd(54,18)=gcd(36,18)=gcd of x,y is 18

test case 2:

Enter two Integer

enter x value

13

The value of x is13

enter y value

101

The value of y is101

gcd(13,101)=gcd(13,88)=gcd(13,75)=gcd(13,62)=gcd(13,49)=gcd(13,36)=gcd(13,23)=gcd(13,10)=gcd(3,10)=gcd(3,7)=gcd(3,4)=gcd(3,1)=gcd(2,1)=gcd of x,y is 1

SCREEN SHOTS

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