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)
JAVA programming language Modify the following code. Make changes so that Movies can be sorted by...
JAVA programming language Modify the following code. Make changes so that Movies can be sorted by title ----------------------------------------------------------------- package Model; import java.time.Duration; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; // TODO - Modify the movie class so that Java knows how to compare two Movies for sorting by title public class Movie extends DataStoreObj { private String title; private String description; private LocalDate releaseDate; private Duration runningTime; private Rating rating; private List<Genre> genres = new ArrayList<>(); private List<Actor> actors = new...
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 =...
**Keil uVision5 - ARM Cortex M0+ - Embedded C programming Modify the program below so that...
**Keil uVision5 - ARM Cortex M0+ - Embedded C programming Modify the program below so that the Red, Green, and Blue LEDs are switched ON and OFF in a sequence with one second delay for Red and Green LEDs, and 0.5 second delay for the Blue LED. #include "MKL25Z4.h" void delay(int n); int main (void) { SIM_SCGC5 |= SIM_SCGC5_PORTB(1); /* enable clock to Port B */ PORTB_PCR18 |=PORT_PCR_MUX(1); /* Configure PORTB ,pin 18 as GPIO ; set MUX*/ GPIOB_PDDR=(1UL <<...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT