-5

I think I have wrote the method correctly but when I try to input a number in the main method and run the script I get no output in console. Please help.

public class Q2_Prime {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    isPrime(19);
}
public static boolean isPrime(int number)
{
    for(int i = 2; i < number; i++)
{
    if(number % i == 0)
        {
            return false;
        }
    }
    
    return true;
}

}

Butta
  • 1
  • 1
  • 2
    You get no output because the code you wrote does not contain a single command to output anything. If you want to print something you'll need to write code for that, eG: `System.out.println(isPrime(19));` This might be a good read: [Differences between System.out.println() and return in Java](https://stackoverflow.com/questions/25456472/differences-between-system-out-println-and-return-in-java) – OH GOD SPIDERS Jun 01 '21 at 15:59

2 Answers2

2

You have to print the returned value to see it.

public class Q2_Prime {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    System.out.println(isPrime(19));
}
public static boolean isPrime(int number)
{
    for(int i = 2; i < number; i++)
{
    if(number % i == 0)
        {
            return false;
        }
    }
    
    return true;
}
}

BTW you can use for(int i = 2; i < number/2; i++) which is more efficient.

ATP
  • 2,939
  • 4
  • 13
  • 34
1

None of your code says anything should be written to the console. Change isPrime(19); to System.out.println(isPrime(19));

Matt U
  • 4,970
  • 9
  • 28