Question

In: Computer Science

CS 206 Visual Programming, Netbeans Understand and modify the attached GCDMethod.java so it can find the...

CS 206 Visual Programming, Netbeans


Understand and modify the attached GCDMethod.java so it can find the greatest common divisor of three numbers input by the user.  Hints: modify the gcd() method by adding the third parameter.

import java.util.Scanner;

public class GCDMethod {
  /** Main method */
  public static void main(String[] args) {
    // Create a Scanner
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter two integers
    System.out.print("Enter first integer: ");
    int n1 = input.nextInt();
    System.out.print("Enter second integer: ");
    int n2 = input.nextInt();

    System.out.println("The greatest common divisor for " + n1 +
      " and " + n2 + " is " + gcd(n1, n2));
  }

  /** Return the gcd of two integers */
  public static int gcd(int n1, int n2) {
    int gcd = 1; // Initial gcd is 1
    int k = 1; // Possible gcd
    
    while (k <= n1 && k <= n2) {
      if (n1 % k == 0 && n2 % k == 0)
        gcd = k; // Update gcd
      k++;
    }

    return gcd; // Return gcd
  }
}
This is the GCDMethod.java attachment

Solutions

Expert Solution

import java.util.Scanner;

public class GCDMethod {
    /** Main method */
    public static void main(String[] args) {
        // Create a Scanner
        Scanner input = new Scanner(System.in);

        // Prompt the user to enter two integers
        System.out.print("Enter first integer: ");
        int n1 = input.nextInt();
        System.out.print("Enter second integer: ");
        int n2 = input.nextInt();
        System.out.print("Enter second integer: ");
        int n3 = input.nextInt();
        System.out.println("The greatest common divisor for " + n1 +
                " and " + n2 + " and "+n3+" is " + gcd(n1, n2,n3));
    }

    /** Return the gcd of two integers */
    public static int gcd(int n1, int n2) {
        int gcd = 1; // Initial gcd is 1
        int k = 1; // Possible gcd

        while (k <= n1 && k <= n2) {
            if (n1 % k == 0 && n2 % k == 0)
                gcd = k; // Update gcd
            k++;
        }

        return gcd; // Return gcd
    }

    public static int gcd(int n1, int n2, int n3) {
        return gcd(n1,gcd(n2,n3));
    }
}


Related Solutions

CS 206 Visual Programming, Netbeans Problem: Write a Java program that displays all the leap years,...
CS 206 Visual Programming, Netbeans Problem: Write a Java program that displays all the leap years, 10 per line, from 1001 to 2100, separated by exactly one space. Also display the total number of leap years in this period. Hints: you need to use a loop ( for-loop is more suitable)
Using NetBeans, Modify your sales application so that it polymorphically processes any account objects that are...
Using NetBeans, Modify your sales application so that it polymorphically processes any account objects that are created. Complete the following:     Create a 2-item array of type Account.     Store each account object created into the array.     For each element in this array, call the calculateSales() method, and use the toString() method to display the results.     Code should be fully commented.     Program flow should be logical. Code before revision: /* * To change this license header, choose...
CS 1102 Unit 5 – Programming AssignmentIn this assignment, you will again modify your Quiz program...
CS 1102 Unit 5 – Programming AssignmentIn this assignment, you will again modify your Quiz program from the previous assignment. You will create an abstract class called "Question", modify "MultipleChoiceQuestion" to inherit from it, and add a new subclass of "Question" called "TrueFalseQuestion". This assignment will again involve cutting and pasting from existing classes. Because you are learning new features each week, you are retroactively applying those new features. In a typical programming project, you would start with the full...
Modify the classes so that it accepts integers instead of characters. (If you understand the concept...
Modify the classes so that it accepts integers instead of characters. (If you understand the concept of generic classes, convert the Node and Linked List classes to a generic so that they can be instantiated with either integers or characters) JAVA Code class aNode { char data; aNode next; aNode(char mydata) { // Constructor data = mydata; next = null; } }; //----------------------------------------------------- class linkedList { aNode head; // Head of the linked list int size; linkedList() { // Constructor...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates the Fibonacci sequence and writes the sequence to the shared-memory object. The Consumer.c file should then output the sequence. Producer.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <zconf.h> int main() { /* The size (in bytes) of shared-memory object */ const int SIZE = 4096; /* The name of shared-memory object */ const char *Obj =...
This is a discussion question for my csc 252 computer programming c++ visual studio so post...
This is a discussion question for my csc 252 computer programming c++ visual studio so post in c++ visual studio so that i can copy and paste thank you. In Object Oriented Programming, classes represent abstractions of real things in our programs. We must quickly learn how to define classes and develop skills in identifying appropriate properties and behaviors for our class. To this end, pick an item that you work with daily and turn it into a class definition....
Please show all work step by step so i can understand Find and classify each critical...
Please show all work step by step so i can understand Find and classify each critical point (as relative maximum, relative minimum, or saddle point) of f(x,y)=2x^3+3x^2+y^2-36x+8y+1
Please Show all work please so I can understand. Given the following information, find the profits...
Please Show all work please so I can understand. Given the following information, find the profits you can make using covered interest arbitrage. Assume you can borrow either EUR 100,000 or JPY 14,619,883.04 EUR interest rate = 3.5% per year JPY interest rate = 0.4530% per year S (EUR/JPY) = EUR 0.00684 per JPY F (EUR/JPY) = EUR 0.0074 per JPY for 1 year maturity forward contract a. Which currency would you borrow to conduct covered-interest arbitrage? b. Assume you...
How to validate Javascript form data? Here is the code. Can someone modify it so that...
How to validate Javascript form data? Here is the code. Can someone modify it so that all the information is validated? Thanks. <!DOCTYPE html> <html lang="en"> <head>    <title>Music Survey</title>    <meta charset="utf-8"> </head> <style>    legend { font-weight:bold;    }    </style> <body> <h1>Music Survey</h1> <form method="post" action=""> <label for="myname"><b>Name:</b></label>        <input type="text" name="myname" id="myname">        <br><br> <label for="myemail"><b>Email:</b></label>        <input type="email" name="myemail" id="myemail">        <br><br>   <fieldset> <legend>Select Your Favorite Types of Music:</legend> <input type="checkbox"...
Can you please write this in python and comment as well so I can understand what...
Can you please write this in python and comment as well so I can understand what yo are doing. Thank you. 1)Loop through list A, at each iteration, show the square root result of that element. Add proper text to your print function. A = [-4, 1, -16, 36, -49, 64, -128] 2)Create a counter variable, name it i and initialize it to 0. Using a for loop, count how many numbers are divisible by 3 in range of 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT