Question

In: Computer Science

Create a Java to find the GCD of two numbers. You will need to create a...

Create a Java to find the GCD of two numbers. You will need to create a new variable to reflect the algorithm (numberOne, numberTwo, GCD = 0).

Requirements:

1) read in numberOne

2) read in numberTwo.

3) run the loop, If numberTwo is 0 then print out numberOne, otherwise, temp =numberOne % numberTwo, numberOne = numberTwo, numberTwo = temp.

Solutions

Expert Solution

import java.util.Scanner;
public class GCD {
    public static void main(String args[]) {
        int numberOne, numberTwo, temp;

        Scanner in = new Scanner(System.in);

        // read in numberOne
        System.out.print("Enter value for numberOne: ");
        numberOne = in.nextInt();

        // read in numberTwo.
        System.out.print("Enter value for numberTwo: ");
        numberTwo = in.nextInt();

        // Checking numberTwo is zero
        while (numberTwo!=0) {
            // temp is the remainder of numberOne and numberTwo
            temp = numberOne % numberTwo;
            // Assigning numberTwo to numberOne
            numberOne = numberTwo;
            // Assigning temp to numberTwo
            numberTwo = temp;
        }

        // Printing the GCD value
        System.out.println("GCD = "+numberOne);
    }
}

 


Related Solutions

9) Create a java programming where you will enter two numbers and create only one method,...
9) Create a java programming where you will enter two numbers and create only one method, which will return or display addition, substraction, multiplication, division, average and check which number is greater than the other. Everything in one method and call it in main method.
JAVA) I need to get 10 integer numbers from the user. Then I need to find...
JAVA) I need to get 10 integer numbers from the user. Then I need to find sum of odd numbers, sum of even numbers, the lowest number of all numbers, the highest number of all numbers, and the average of all numbers( use double, with the two digit decimal) process; loopCount = 1 While LoopCount <= 10 Read number from the keyboard If odd, add to the total of all odd numbers If even, add to the total of all...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
in java we need to order a list , if we create a program in java...
in java we need to order a list , if we create a program in java what  are the possible ways of telling your program how to move the numbers in the list to make it sorted, where each way provides the required result. list the name of sorting with short explanation
C++ Question Create two circular linked lists and find their maximum numbers. Merge the two circular...
C++ Question Create two circular linked lists and find their maximum numbers. Merge the two circular linked lists such that the maximum number of 2nd circular linked list immediately follows the maximum number of the 1st circular linked list. Input: 12 -> 28 -> 18 -> 25 -> 19-> NULL 5 -> 24 -> 12 -> 6 -> 15-> NULL Output: 28 -> 24-> 25 -> 15 -> 19 -> 15->5-> 18 -> 25 -> 19->NULL Note:- Code should work...
For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
Java question, not sure how to do this... You need to create a base class that...
Java question, not sure how to do this... You need to create a base class that should have the following functionality. - Calculate avg. Students grade. Input parameter to this method is an array that shows grades for at least ten courses. You need to create a child class that inherits the base class. The child class should have a method to calculate max grade from 10 courses. You need to write a demo class. The demo class should have...
You need to create a Java class library to support a program to simulate a Point-...
You need to create a Java class library to support a program to simulate a Point- of-Sale (POS) system. General Requirements: You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; You should create identifiers with sensible names; You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. Logical structures...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT