1 Replies - 16577 Views - Last Post: 30 October 2010 - 05:39 PM Rate Topic: -----

#1 jmorche  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-October 10

isPrime Method, Program to determine if a number is prime

Posted 30 October 2010 - 05:29 PM

I am being asked to determine if a number that the user enters is prime or not by using a method. We are encouraged to use a boolean type method.
I believe I have this structured correctly, but it does not compile.
I am unsure of a few things that I have done.
1) I do not have to "call" my method, because it runs at the bottom and returns a value.
but can the main method reach down at retrive the information with out calling it?
2) How do I handle the boolean value in the main method. A boolean returns a true, otherwise
it is false. I do not know how to structure to expression to use it properly.
3) The compiler does not like my "else" in the middle of the main method.
Any kick in the right direction is appreciated.
Thank you

import java.util.Scanner;

public	class IsPrimeMethod

{
	public	static	void	main(String []args)

  {
  		int 	number;	// declare number as an int
		
		Scanner keyboard	=	new	Scanner(System.in); // create a scanner object
		
		// ask user for the number to check for prime
		System.out.println("Please enter a number and the computer will"  + 
					"\n determine if it is a prime number.\n");
		number = keyboard.nextInt();
		
	 
	
		if(isPrime);   // check to see if method is true
	   {
		  System.out.println("Your number is not prime");
		}
		
		else      // if method is false
		{
			System.out.println("Your number is prime");
		}
		
		
	}
	
	public static boolean isPrime(int number)  // method isPrime
	{
		for(int index = 2; index<number; index++)  // loop counter, to check if a number will divide into
		{                                          // the input number, starting at two and going until one short of input number 
		 if(number%index == 0)   // use mod operator to check if a number will divide into equally
			   status = true;
		  else
			   status = false;   // use mod operator to check if a number will divide into equally
 										// if no number will divide into equally then it is a prime number
		
		 return status;			// return value
			
		}
			
	}		
  
}





Is This A Good Question/Topic? 0
  • +

Replies To: isPrime Method, Program to determine if a number is prime

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: isPrime Method, Program to determine if a number is prime

Posted 30 October 2010 - 05:39 PM

isPrime is a function, you need to call it with parentheses and parameter(s).

if(isPrime(number))
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1