Prime Number between 100-999 in Java


The Java program prints all the prime numbers from 1 to 999.

  • The program starts by importing the Scanner class from the java.util package, which is not used in the program and can be removed.
  • Then, the program initializes a variable f to 0, which will be used to count the number of factors of each number.
  • The program uses two nested for loops to iterate through all numbers from 1 to 999. The outer loop iterates through all the numbers, while the inner loop checks whether each number is a prime number by counting the number of factors.
  • Inside the inner for loop, the program checks whether each number from 1 to n is a factor of n. If n is exactly divisible by a number, then the f variable is incremented. At the end of the loop, f will contain the total number of factors of n.
  • If the value of f is exactly 2, then n has only two factors (1 and n), and therefore, it is a prime number. In this case, the program prints the number to the console.
  • Finally, the program resets the value of f to 0 before continuing to the next number.

Source Code

import java.util.Scanner;
public class prime_numbers {
    //Write a program to print the prime numbers between 1 to 999
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int f = 0;
        for(int n=1;n<=999;n++) {
            for (int i = 1; i <= n; i++) {
                if (n % i == 0) {
                    f++;
                }
            }
            if (f == 2) {
                System.out.println(n + " is a Prime Number");
            }
            f=0;
        }
 
    }
}
 

Output

2 is a Prime Number
3 is a Prime Number
5 is a Prime Number
7 is a Prime Number
11 is a Prime Number
13 is a Prime Number
17 is a Prime Number
19 is a Prime Number
23 is a Prime Number
29 is a Prime Number
31 is a Prime Number
37 is a Prime Number
41 is a Prime Number
43 is a Prime Number
47 is a Prime Number
53 is a Prime Number
59 is a Prime Number
61 is a Prime Number
67 is a Prime Number
71 is a Prime Number
73 is a Prime Number
79 is a Prime Number
83 is a Prime Number
89 is a Prime Number
97 is a Prime Number
101 is a Prime Number
103 is a Prime Number
107 is a Prime Number
109 is a Prime Number
113 is a Prime Number
127 is a Prime Number
131 is a Prime Number
137 is a Prime Number
139 is a Prime Number
149 is a Prime Number
151 is a Prime Number
157 is a Prime Number
163 is a Prime Number
167 is a Prime Number
173 is a Prime Number
179 is a Prime Number
181 is a Prime Number
191 is a Prime Number
193 is a Prime Number
197 is a Prime Number
199 is a Prime Number
211 is a Prime Number
223 is a Prime Number
227 is a Prime Number
229 is a Prime Number
233 is a Prime Number
239 is a Prime Number
241 is a Prime Number
251 is a Prime Number
257 is a Prime Number
263 is a Prime Number
269 is a Prime Number
271 is a Prime Number
277 is a Prime Number
281 is a Prime Number
283 is a Prime Number
293 is a Prime Number
307 is a Prime Number
311 is a Prime Number
313 is a Prime Number
317 is a Prime Number
331 is a Prime Number
337 is a Prime Number
347 is a Prime Number
349 is a Prime Number
353 is a Prime Number
359 is a Prime Number
367 is a Prime Number
373 is a Prime Number
379 is a Prime Number
383 is a Prime Number
389 is a Prime Number
397 is a Prime Number
401 is a Prime Number
409 is a Prime Number
419 is a Prime Number
421 is a Prime Number
431 is a Prime Number
433 is a Prime Number
439 is a Prime Number
443 is a Prime Number
449 is a Prime Number
457 is a Prime Number
461 is a Prime Number
463 is a Prime Number
467 is a Prime Number
479 is a Prime Number
487 is a Prime Number
491 is a Prime Number
499 is a Prime Number
503 is a Prime Number
509 is a Prime Number
521 is a Prime Number
523 is a Prime Number
541 is a Prime Number
547 is a Prime Number
557 is a Prime Number
563 is a Prime Number
569 is a Prime Number
571 is a Prime Number
577 is a Prime Number
587 is a Prime Number
593 is a Prime Number
599 is a Prime Number
601 is a Prime Number
607 is a Prime Number
613 is a Prime Number
617 is a Prime Number
619 is a Prime Number
631 is a Prime Number
641 is a Prime Number
643 is a Prime Number
647 is a Prime Number
653 is a Prime Number
659 is a Prime Number
661 is a Prime Number
673 is a Prime Number
677 is a Prime Number
683 is a Prime Number
691 is a Prime Number
701 is a Prime Number
709 is a Prime Number
719 is a Prime Number
727 is a Prime Number
733 is a Prime Number
739 is a Prime Number
743 is a Prime Number
751 is a Prime Number
757 is a Prime Number
761 is a Prime Number
769 is a Prime Number
773 is a Prime Number
787 is a Prime Number
797 is a Prime Number
809 is a Prime Number
811 is a Prime Number
821 is a Prime Number
823 is a Prime Number
827 is a Prime Number
829 is a Prime Number
839 is a Prime Number
853 is a Prime Number
857 is a Prime Number
859 is a Prime Number
863 is a Prime Number
877 is a Prime Number
881 is a Prime Number
883 is a Prime Number
887 is a Prime Number
907 is a Prime Number
911 is a Prime Number
919 is a Prime Number
929 is a Prime Number
937 is a Prime Number
941 is a Prime Number
947 is a Prime Number
953 is a Prime Number
967 is a Prime Number
971 is a Prime Number
977 is a Prime Number
983 is a Prime Number
991 is a Prime Number
997 is a Prime Number
To download raw file Click Here

Basic Programs