Question

Been working on this program for hours and keep getting error. PLEASE help and show a...

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 */
        }
}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Been working on this program for hours and keep getting error. PLEASE help and show a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

  • This is for a java program public class Calculation {    public static void main(String[] args)...

    This is for a java program public class Calculation {    public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object    //Task 1. Write your code here //Print the prompt and ask the user to enter an...

  • using this code to complete. it python 3.#Display Program Purpose print("The program will show how much...

    using this code to complete. it python 3.#Display Program Purpose print("The program will show how much money you will make working a job that gives a inputted percentage increase each year") #Declaration and Initialization of Variables userName = "" #Variable to hold user's name currentYear = 0 #Variable to hold current year input birthYear = 0 #Variable to hold birth year input startingSalary = 0 #Variable to hold starting salary input retirementAge = 0 #Variable to hold retirement age input...

  • // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class...

    // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class (SwitchDoLab) //--> {    //   Declare the main method    //-->    {        //   Declare Constant integers SUM = 1, FACTORIAL = 2, QUIT = 3.        //-->        //-->        //-->        //   Create an integer variable named choice to store user's option.        //-->        //   Create a Scanner object        //   Create...

  • Need help with implementing a While, Do-while and for loops in a program. UML: - scnr...

    Need help with implementing a While, Do-while and for loops in a program. UML: - scnr : Scanner - rows : int - MAX_ASCII : int ------------------------------------------------------------------------------- + CharacterTables() : + CharacterTables(rows : int) : - getStartingValue() : int -displayTable(startValue : int) : void - userContinue() : boolean + main(args : String[]) : void In Section 5.10 we saw that there is a close relationship between char and int. Variables of type char store numbers. (Actually, variables of all types...

  • This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total...

    This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total Also I would need a new name for the daubles. An example here might be double tRtalD-caradd xD, YD) 1. (a) We are going to begin with some simple math, reading input, and formatted output. Create a class called MuCalsustec Yau will not have an instance variable. Since you have no instance variables to initialize, you do nat need a constructar. Do NOT create...

  • Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the...

    Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the mean and standard deviation of these numbers I already have the source code all done. I just need this translated into a flowchart. Again I just need the flowchart, not any source code. I have already provided the source code below. Need Flowchart version of my code Here is the source code: public class Mean_Standard_Deviation {    public static void main(String[] args) {   ...

  • Create three variables for an int array, the size of the array, and a Scanner object...

    Create three variables for an int array, the size of the array, and a Scanner object to read keyboard input. Ask the user to specify the size of the array and then construct an int array of that size. Next, use a for loop to continuously ask the user to specify values for each location in the array. Lastly, print contents of the array as a histogram using the asterisk character (*). This will require using nested for loops. The...

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT