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

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...