Wednesday, December 5, 2018

write an algorithm to print prime number from 1 to specific number.

public class PrintAllPrime {
    public static void main(String args[]) {
        int num = 22;
        int count;
        for (int i = 2; i <= num; i++) {
            count = 2;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    count++;
                    break;
                }
            }
            if (count == 2) {
                System.out.print(i + " ");
            }
        }
    }
}
Output:
2 3 5 7 11 13 17 19

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...