6 Replies - 2040 Views - Last Post: 30 September 2012 - 10:36 PM Rate Topic: -----

#1 bnjmn556  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • 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++){}


Was This Post Helpful? 0
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: For Loop for Greatest Common Divisor

Posted 30 September 2012 - 09:25 PM

View Postbnjmn556, 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

Was This Post Helpful? 0
  • +
  • -

#4 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: For Loop for Greatest Common Divisor

Posted 30 September 2012 - 09:29 PM

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

View Postbnjmn556, 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
Was This Post Helpful? 1
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: For Loop for Greatest Common Divisor

Posted 30 September 2012 - 09:45 PM

View Postnatecat, on 01 October 2012 - 09:59 AM, said:

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

View Postbnjmn556, 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
Was This Post Helpful? 0
  • +
  • -

#6 bnjmn556  Icon User is offline

  • New D.I.C Head

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

Re: For Loop for Greatest Common Divisor

Posted 30 September 2012 - 10:02 PM

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

View Postbnjmn556, 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++){ ?
Was This Post Helpful? 0
  • +
  • -

#7 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • 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
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1