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 */
}
}
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.
Been working on this program for hours and keep getting error. PLEASE help and show a...
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) { 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...
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...
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...
Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) { // average function , declaring variable int i; char str[40]; float avg = 0; // iterating in...
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...
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...
// 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...
Please help and please satisfy the directions. The directions are under program 3. port JavaScore. public class Example 4 [ public static int getint String strum - JOptionPane.showinput Dialog("Enter an integer"); return integer.parseInt(strum): public static void main(String args Scanner input = new Scanner (System.in): System.out.println("Enter 2 integers"); intx input.nextInt(); inty input.nextInt(): 1/ these are simple is if b ) System.out.println("The first number is positive): if (0 y0) System.out.println("Both numbers are positive"); ( 11 y) System.out.println("At least one number is positive");...
I have the following java-program, that creates a random walk starting at (0,0) and continuing until x or y is greater than abs(n). First: I get an error, when I try closing the Scanner by inserting "reader.close(); " in line 28. It says "Unreachable code". How can I fix that? Second: Is it possible to create a different colour for the StdDraw.point when it reaches a point on one of the edges "n+1" or "n-1" ? 1 import java.util.*; 3...