-1

Just curious is there a way to set an if statement condition that checks if value is equal to a data type. Like if (x=int) kind of thing.

I am suppose to create a program that checks for prime numbers but i do not know how to do it other than divide the number(entered by user) by the building block number (2-9(excluding 1 because all numbers are divisible ...)) and if all of them return a float then the number is prime.

I also think this way is inefficient but again i do not have any other clue how i would do it. So if possible some ideas would work too.

This is the structure of the code:

package innocence;
import java.util.*;

    class GS1
    {

            public static void main(String[]args)
             {
                Scanner input = new Scanner(System.in);  
                double num;
                System.out.println("Enter a number to check if it is prime or not");
                num=input.nextDouble();


                if((num % 2 == float) || (num % 3== float) || (num % 4 == float)|| (num % 5 == float)|| (num % 6 == float)|| (num % 7 == float)|| (num % 8 == float)|| (num % 9 == float) ) // float returns an error but i want to do it so it checks if it is a float or not
                   {
                      System.out.println("The number you haver entered," + num + "is a prime number");
                   }        

                else
                   {
                  System.out.println("The number entered is not prime");
                   }

            }
    }  
Abhishek Saxena
  • 292
  • 3
  • 15
Jayden Freh
  • 99
  • 1
  • 2
  • 7

1 Answers1

0

You can check if the number is equal to the number rounded to the nearest integer:

public static boolean isInteger(double n) {
    return n == Math.round(n);
}

if(!isInteger(num % 2) && !isInteger(num % 3) && !isInteger(num % 4) &&
   !isInteger(num % 5) && !isInteger(num % 6) && !isInteger(num % 7) &&
   !isInteger(num % 8) && !isInteger(num % 9) )
   {
      System.out.println("The number you haver entered," + num + "is a prime number");
   }        

else
   {
  System.out.println("The number entered is not prime");
   }

If course this doesn't really check if the number is prime or not; it only checks whether the number is divisible by the integers 2 to 9.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Thanks alot. i don't understand it that much because i started java recently and was trying to challenge myself(using an inefficient math way i guess). I'm going to go research integrals and stuff. – Jayden Freh Jul 03 '15 at 01:49
  • If the input number is actually an integer, e.g. `13.0`, then will always print `Not Prime`. It just passes an integer into `integral` and then checks that it rounds to itself. – Windle Jul 03 '15 at 02:15
  • @Windle As I said, I didn't adjust his incorrect logic. I only looked at the part where he wanted to check whether the result of the `%` operation was a fractional number. `13.0 % 2 = 1.0` so it's not a fractional number. I guess what he should check is at least whether `(num % 2 == 0)` but then the whole logic is nowhere near correct to check for primes. He should use a sieve for that. – Erwin Bolwidt Jul 03 '15 at 02:22
  • @ErwinBolwidt Fair enough. And the renaming to `isInteger` helps too; was a little worried he was going to try and learn integrals for this =) – Windle Jul 03 '15 at 02:37