22:38

Prime Number

Posted by Mitesh Patel |


class PrimeNumberExample {

    void prime(int num) {

        int i;
        int flag = 0;

        for (i = 2; i < num; i++) {
            if (num % i == 0) {
                flag = 1; //flag= 1 not prime
            }
        }


        if (flag == 0) {
            System.out.println("prime = " + num);

        }
    }

    public static void main(String args[]) {

        int i;
        PrimeNumberExample x = new PrimeNumberExample();
        for (i = 1; i <= 100; i++) {
            x.prime(i);
        }

    }
}

0 comments:

Post a Comment

Subscribe