# For Loop for Greatest Common Divisor

Page 1 of 1

## 6 Replies - 2700 Views - Last Post: 30 September 2012 - 10:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293783&amp;s=81ee9d7159e6b9cec7fd7471dba8c06a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bnjmn556

Reputation: 0
• Posts: 2
• Joined: 30-September 12

# For Loop for Greatest Common Divisor

Posted 30 September 2012 - 08:58 PM

Hey all, first time poster here.

Im taking an intro to Java class and Ive been wracking my brain trying to figure this one out.

I have to rewrite this code provided from the teacher to use a "For Loop" instead of the "While Loop" to find the GCD of an integer entered by the user. I have no clue how to use a "For Loop" or where to even get started. Any help is appreciated.

```
import java.util.Scanner;
public class Lab4Part3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
int divisor;

System.out.println("Enter an integer greater than 1");
num = input.nextInt();

divisor = num / 2;
while (num % divisor != 0) {
System.out.println("Trying factor " + divisor);
divisor--;
}
if (divisor == 1) {
System.out.print("The input integer " + num + " is a prime number");
} else {
System.out.print("The input integer " + num + " is divisible by " + divisor);
}
}

```

Is This A Good Question/Topic? 0

## Replies To: For Loop for Greatest Common Divisor

### #2 natecat

Reputation: 56
• Posts: 233
• Joined: 19-December 11

## Re: For Loop for Greatest Common Divisor

Posted 30 September 2012 - 09:21 PM

A for loop is basicly a loop that runs a certain amount of times. The format for a for loop is this:
```for(variable declaration;condition;variable increment){}
```

For example this:
```int a=0
while(a<10){
a++
}

```

is equvilent to this:
```for(int a=0;a<10;a++){}

```

### #3 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,440
• Joined: 14-September 12

## Re: For Loop for Greatest Common Divisor

Posted 30 September 2012 - 09:25 PM

bnjmn556, on 01 October 2012 - 09:28 AM, said:

Hey all, first time poster here.

Im taking an intro to Java class and Ive been wracking my brain trying to figure this one out.

I have to rewrite this code provided from the teacher to use a "For Loop" instead of the "While Loop" to find the GCD of an integer entered by the user. I have no clue how to use a "For Loop" or where to even get started. Any help is appreciated.

```
import java.util.Scanner;
public class Lab4Part3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
int divisor;

System.out.println("Enter an integer greater than 1");
num = input.nextInt();

divisor = num / 2;
while (num % divisor != 0) {
System.out.println("Trying factor " + divisor);
divisor--;
}
if (divisor == 1) {
System.out.print("The input integer " + num + " is a prime number");
} else {
System.out.print("The input integer " + num + " is divisible by " + divisor);
}
}

```

Let me explain to you what a 'for' loop is.

Syntax:

```for( initialization; condition; increment)
{
/*code*/
}
```

eg. If you want to list the numbers from 1 to 100

``` for( int num=1; num<=100;num++)
{
System.out.println("+num");
}

```

Now, sometimes 'for' loop can also be used as an infinite loop as follows.

```for(;;)/>{/*This is not advisable in good programming*/}
```

regards,
Raghav

This post has been edited by raghav.naganathan: 30 September 2012 - 09:26 PM

### #4 natecat

Reputation: 56
• Posts: 233
• Joined: 19-December 11

## Re: For Loop for Greatest Common Divisor

Posted 30 September 2012 - 09:29 PM

raghav.naganathan, on 30 September 2012 - 09:25 PM, said:

bnjmn556, on 01 October 2012 - 09:28 AM, said:

Hey all, first time poster here.

Im taking an intro to Java class and Ive been wracking my brain trying to figure this one out.

I have to rewrite this code provided from the teacher to use a "For Loop" instead of the "While Loop" to find the GCD of an integer entered by the user. I have no clue how to use a "For Loop" or where to even get started. Any help is appreciated.

```
import java.util.Scanner;
public class Lab4Part3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
int divisor;

System.out.println("Enter an integer greater than 1");
num = input.nextInt();

divisor = num / 2;
while (num % divisor != 0) {
System.out.println("Trying factor " + divisor);
divisor--;
}
if (divisor == 1) {
System.out.print("The input integer " + num + " is a prime number");
} else {
System.out.print("The input integer " + num + " is divisible by " + divisor);
}
}

```

Let me explain to you what a 'for' loop is.

Syntax:

```for( initialization; condition; increment)
{
/*code*/
}
```

eg. If you want to list the numbers from 1 to 100

``` for( int num=1; num<=100;num++)
{
System.out.println("+num");
}

```

Now, sometimes 'for' loop can also be used as an infinite loop as follows.

```for(;;)/>{/*This is not advisable in good programming*/}
```

regards,
Raghav

I just wanted to point out that his example count to 100 code won't work unless you remove the quotes around +num and make the intitial variable equal to 0 not 1

### #5 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,440
• Joined: 14-September 12

## Re: For Loop for Greatest Common Divisor

Posted 30 September 2012 - 09:45 PM

natecat, on 01 October 2012 - 09:59 AM, said:

raghav.naganathan, on 30 September 2012 - 09:25 PM, said:

bnjmn556, on 01 October 2012 - 09:28 AM, said:

Hey all, first time poster here.

Im taking an intro to Java class and Ive been wracking my brain trying to figure this one out.

I have to rewrite this code provided from the teacher to use a "For Loop" instead of the "While Loop" to find the GCD of an integer entered by the user. I have no clue how to use a "For Loop" or where to even get started. Any help is appreciated.

```
import java.util.Scanner;
public class Lab4Part3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
int divisor;

System.out.println("Enter an integer greater than 1");
num = input.nextInt();

divisor = num / 2;
while (num % divisor != 0) {
System.out.println("Trying factor " + divisor);
divisor--;
}
if (divisor == 1) {
System.out.print("The input integer " + num + " is a prime number");
} else {
System.out.print("The input integer " + num + " is divisible by " + divisor);
}
}

```

Let me explain to you what a 'for' loop is.

Syntax:

```for( initialization; condition; increment)
{
/*code*/
}
```

eg. If you want to list the numbers from 1 to 100

``` for( int num=1; num<=100;num++)
{
System.out.println("+num");
}

```

Now, sometimes 'for' loop can also be used as an infinite loop as follows.

```for(;;)/>{/*This is not advisable in good programming*/}
```

regards,
Raghav

I just wanted to point out that his example count to 100 code won't work unless you remove the quotes around +num and make the intitial variable equal to 0 not 1

I am sorry for my mistake. Thanks a lot for correcting me, natecat.

regards,
Raghav

### #6 bnjmn556

Reputation: 0
• Posts: 2
• Joined: 30-September 12

## Re: For Loop for Greatest Common Divisor

Posted 30 September 2012 - 10:02 PM

raghav.naganathan, on 30 September 2012 - 09:25 PM, said:

bnjmn556, on 01 October 2012 - 09:28 AM, said:

Hey all, first time poster here.

Im taking an intro to Java class and Ive been wracking my brain trying to figure this one out.

I have to rewrite this code provided from the teacher to use a "For Loop" instead of the "While Loop" to find the GCD of an integer entered by the user. I have no clue how to use a "For Loop" or where to even get started. Any help is appreciated.

```
import java.util.Scanner;
public class Lab4Part3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
int divisor;

System.out.println("Enter an integer greater than 1");
num = input.nextInt();

divisor = num / 2;
while (num % divisor != 0) {
System.out.println("Trying factor " + divisor);
divisor--;
}
if (divisor == 1) {
System.out.print("The input integer " + num + " is a prime number");
} else {
System.out.print("The input integer " + num + " is divisible by " + divisor);
}
}

```

Let me explain to you what a 'for' loop is.

Syntax:

```for( initialization; condition; increment)
{
/*code*/
}
```

eg. If you want to list the numbers from 1 to 100

``` for( int num=1; num<=100;num++)
{
System.out.println("+num");
}

```

Now, sometimes 'for' loop can also be used as an infinite loop as follows.

```for(;;)/>{/*This is not advisable in good programming*/}
```

regards,
Raghav

Im a complete noob to programming so forgive my ignorance. So in the previous code I would replace the while, if, and else statements with just one "For loop"?

e.g. for (num = 0; divisor = num / 2; num++){ ?

### #7 natecat

Reputation: 56
• Posts: 233
• Joined: 19-December 11

## Re: For Loop for Greatest Common Divisor

Posted 30 September 2012 - 10:36 PM

No, its more like
```divisor=num/2;
for(;num%divisor==0;divisor--){
//code goes here
}

```