Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java prime numbers


for(int i = 2 ; i <= num ; i++)
        {
            if(isPrime(i))
            {
                System.out.print("  " + i);
                cnt++;
            }
        }
        System.out.println("Prime numbers = " + cnt);
    }

    public static boolean isPrime(int i) {
        for (int j = 2; j <= Math.sqrt(i); j++) {

            if(i % j == 0)
            {
                return false;
            }

        }
        return true;
    }
 
PREVIOUS NEXT
Tagged: #java #prime #numbers
ADD COMMENT
Topic
Name
1+7 =