Programming





Write a Program to find all the prime numbers between 1 and 100

Write a Program to find all the prime numbers between 1 and 100


by Administrator | 25-Mar-2020
Java

Tags:  Java  Program  Prime Number

Share: 


Program

public class PrimeNumber {

    public static void main(String[] args) {

        int limit = 100; // define limit
        System.out.println("Prime numbers between 1 and " + limit + " are: ");
        // loop through the numbers one by one
        for (int i = 1; i < limit; i++) {
            boolean isPrime = true;
            // check to see if the number is prime
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break; // exit the inner for loop
                }
            }
            if (isPrime) // print the number if prime
                System.out.print(i + " ");
        }
    }
}

Output


Prime numbers between 1 and 100 are: 
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.