3 Replies - 33457 Views - Last Post: 20 February 2011 - 11:18 PM Rate Topic: -----

#1 twistedspoon   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-February 11

Determine if 2 Numbers Are Co-prime Using Loops

Posted 20 February 2011 - 10:38 PM

"Your program will prompt the user for a single integer input. Then it will determine
all the numbers between 2 and 100 (inclusive) which are co-prime with respect to the user-input number.

Construct a program that:
1. prompts user to enter a single integer number using the JOptionPane input dialog API
2. loops for all numbers between 2 and 100 (inclusive) and prints only those numbers which are co-prime
with the user-input number (using System.out"

this is what I have so far:

import javax.swing.JOptionPane;

public class loops {
	public static void main(String[] args) {
	String input = JOptionPane.showInputDialog( "Enter a single integer:" );//asks user to input the variable
	int userInt = Integer.parseInt( input );//parses user's input into integer form
	
	for (int coprime = 2; coprime <= 100; coprime = coprime+1){
		for (int divisor = 2; divisor <= userInt/2; divisor = divisor+1){
			if ((userInt % divisor == 0)&&(coprime % divisor == 0)) continue; 
			else System.out.println(coprime);
						
		}
		
	}
	
	}

}



I honestly have no clue what I'm doing. The code compiles but it definitely does not give me the correct output.

Is This A Good Question/Topic? 0
  • +

Replies To: Determine if 2 Numbers Are Co-prime Using Loops

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Determine if 2 Numbers Are Co-prime Using Loops

Posted 20 February 2011 - 10:44 PM

Two numbers are coprime if and only if they share no common factors > 1. Hint- check if gcf(a, B) == 1. If it does, a and b are coprime.
Was This Post Helpful? 0
  • +
  • -

#3 twistedspoon   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-February 11

Re: Determine if 2 Numbers Are Co-prime Using Loops

Posted 20 February 2011 - 11:14 PM

View Postmacosxnerd101, on 20 February 2011 - 10:44 PM, said:

Two numbers are coprime if and only if they share no common factors > 1. Hint- check if gcf(a, B) == 1. If it does, a and b are coprime.


how can I check that? the way you phrased that made me think
import javax.swing.JOptionPane;

public class loops {
	public static void main(String[] args) {
	String input = JOptionPane.showInputDialog( "Enter a single integer:" );//asks user to input the variable
	int userInt = Integer.parseInt( input );//parses user's input into integer form
	
	for (int coprime = 2; coprime <= 100; coprime = coprime+1){
		if gcf(userInt, coprime) == 1 System.out.println(coprime);
						
		}
		
	}
	
	
}


would work, but I think I'm just making stuff up with that.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Determine if 2 Numbers Are Co-prime Using Loops

Posted 20 February 2011 - 11:18 PM

Take a look at the Euclidean GCF algorithm for finding the GCF of two numbers. In fact, I've written a snippet on the Euclidean algorithm. You will have to define a gcf() method in your class.

Don't forget that if statements have parentheses around the condition. So this if gcf(userInt, coprime) == 1, should read as if(gcf(userInt, coprime) == 1).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1