-7

We have an assignment in class to create a greatest common divider (gcd) program using functions. I missed out on the lesson where we learned how to properly use them. I finished the part that actually does the division but I don't know how to separate it into a function and have it work. I'd like to have the input in the main class and the process in function.

This is what I have, it does not work when I run it

package gcd.function.java.program;

import java.util.Scanner;

/**
*
* @author sarah_000
*/
public class GCDFunctionJavaProgram {

public static void main(String[] args) {

     int num1;
     int num2;
     int div;
    Scanner input = new Scanner(System.in);

    System.out.print("Enter your first number: ");
    num1 = input.nextInt();
    System.out.print("Enter your second number: ");
    num2 = input.nextInt();

    System.out.printf("The GCD is %d ", div);
}

public static void GCDFunction() {

    if(num1 > num2)
       div = num2;

   else div = num1;

   while((num1 % div!= 0)||(num2 % div != 0))
   {
   div --;
   }//end of while loop

   }    
}

Any tips or help you can give to me will be greatly appreciated, I'm very new

1
  • 2
    "it does not work" is not a useful problem description. Please edit your question to include how your program doesn't work, including the full text of any errors, the input you provided, the output you expected and the output you actually got.
    – azurefrog
    Nov 30, 2015 at 19:38

3 Answers 3

1

You declare two parameters and modify the return type in your GCDFunction like this:

public static int GCDFunction(int num1, int num2) 

You are currently trying to access the variables in the main method but are out of scope.

Also, you never actually call your GCDFunction

0

Think of it like passing functions in math. The GCDFunction() has to receive the numbers into the function so we do

public static void GCDFunction(int num1, int num2)

That also lets Java know the type it is, type int. And your java variables are scoped inside of the functions so you have to print the output in the function that created the variable in your scenario.

So once you have that function set up to receive the variables and output after processing, you call the function in the main with a

GCDFunction(num1, num2);

Where num1 and num2 are the variables that have your integers stored in.

The end result after a little rearranging looks like this.

import java.util.Scanner;

public class GCDFunctionJavaProgram {

public static void main(String[] args) {

    int num1;
    int num2;

    Scanner input = new Scanner(System.in);

    System.out.print("Enter your first number: ");
    num1 = input.nextInt();
    System.out.print("Enter your second number: ");
    num2 = input.nextInt();

    GCDFunction(num1, num2);

}

public static void GCDFunction(int num1, int num2) {
    int div;
    if(num1 > num2){
       div = num2;
    }

   else{ div = num1;}

   while((num1 % div!= 0)||(num2 % div != 0))
   {
   div --;
   }//end of while loop
   System.out.printf("The GCD is %d ", div);
   }
3
  • 1
    should you not at least print the gcd number produced why do all that and not print it like the op wants?
    – dave
    Nov 30, 2015 at 19:51
  • Thanks. Forgot to move the statement after I deleted it from the main. Corrected.
    – SoulOfSet
    Nov 30, 2015 at 19:54
  • Thank you, your explanation helped make it much clearer!
    – SarahRosey
    Nov 30, 2015 at 21:33
0

Trying to give you a example of how the code should be to take in variable number of parameters.

public int gcd(Integer... numbers) {
    int gcd = 1;
    int miNNumber=Collections.min(Arrays.asList(numbers));
    boolean isDivisible;
    for(int i=2; i<=miNNumber;i++) {
        isDivisible=true;
        for(Integer eachNumber : numbers) {
            if(eachNumber%i!=0) {
                isDivisible=false;
                break;
            }
        }
        if(isDivisible) {
            gcd=i;
        }
    }
    return gcd;
}

You can call it

gcd(10, 200, 400);

or

gcd(10, 200, 400, 4000, 40);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.