Hi: I'm trying to solve a question as it states: "Create a program which determines if a given number a is a common divisor of two other numbers num1 and num2." I am 100% beginner to Java and just started a couple days ago, so most terms discussed feel quite foreign to me, and so i usually need a little extra explaining. This is what I have come up with so far, even though I know it is not right, as i get errors.

/**
 * AWT Sample application
 *
 * @author 
 * @version 1.00 09/09/10
 */
public class Common_divisor {
    
    public static void main(String[] args) {
     int a; 
     int num2; 
     int num1; 
     if (a % num2 = 0 && a % num1 = 0 )
     System.out.println("Common Divisor");
     else
     System.out.println("Not Common Divisor");
    }
}

Above is what i have come up with so far. I'm not sure which is the best way going about answering the question, be it by attaching a scanner, using random numbers, etc.

Your help is always appreciated, thanks a ton in advance.

Recommended Answers

All 2 Replies

You need to give values to your variables:

int a; 
     int num2; 
     int num1;

I suggest you enter put some values inside the code in order to test if the logic is correct:

int a = 2; 
     int num2; = 4 
     int num1 = 10;

And then use the Scanner class to get the input from the keyboard:

Scanner scan = new Scanner(System.in);
if (scan.hasNextInt() ) {
   a = scan.nextInt();
} else {
   System.out.println("No number given");
   System.exit(1);
}

Also read this first
http://java.sun.com/javase/6/docs/api/java/util/Scanner.html

the use is like this

if ((a % num2 ==0)&&(a%num1==0))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.