jeudi 2 juillet 2015

conditional statement using data types

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");
               }

        }
}  

Aucun commentaire:

Enregistrer un commentaire