Java simple example of finding a prime number of prime Numbers within 100

  • 2020-04-01 03:13:55
  • OfStack

Prime Numbers are also called prime Numbers. A natural number greater than 1, if it is not divisible by any other natural number except 1 and itself; Otherwise it's called composite. According to the fundamental theorem of arithmetic, every integer greater than 1 is either a prime in itself or can be written as a product of a series of primes; And if you don't think about the order of primes in the product, then it's unique. The following is a simple Java example of finding primes up to 100


public class test {
 public static void main(String[] args) {
  int i,n,k=0;
     for (n = 3; n<=100; n++) {     //Everything from 3 to 100
         i=2;
         while (i<n) {
             if (n%i==0)  break;  //If n is not a prime number, we can break out of the current loop
             i++;
          }

         
      if (i==n) {     //If I ==n then n is not divisible by 2~n-1 and is prime
             k++;             //Count the number of output Numbers
             System.out.print(i+ "t ");
             if (k %6==0)    //Line breaks for every 5 output
              System.out.println();
         }
     }
 }
}


Related articles: