# Prime Number From 1 To 100 Program In Java - See How!

**URL:** <https://onehack.st/t/prime-number-from-1-to-100-program-in-java-see-how/57664>\
**Category:** Tutorials & Methods\
**Tags:** tips-tricks\
**Created:** [September 15, 2019, 8:33pm UTC](https://onehack.st/t/prime-number-from-1-to-100-program-in-java-see-how/57664 "2019-09-15T20:33:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aina](https://onehack.st/user_avatar/onehack.st/aina/32/167469_2.png) [@Aina](https://onehack.st/u/Aina)\
**Post date:** [September 15, 2019, 8:33pm UTC](https://onehack.st/t/prime-number-from-1-to-100-program-in-java-see-how/57664/1 "2019-09-15T20:33:32Z")

</div>

## What is a Prime Number?

A prime number is a number that is only divisible by 1 or itself. For example, 11 is only divisible by 1 or itself. Other Prime numbers 2, 3, 5, 7, 11, 13, 17… _(Source: Guru99)_

**Note** : 0 and 1 are not prime numbers. 2 is the only even prime number.

Java Program to display prime numbers from 1 to 100

**Program Logic** :

- The main method contains a loop to check prime numbers one by one.
- The main method calls the method CheckPrime to determine whether a number is prime
- We need to divide an input number, say 17 from values 2 to 17 and check the remainder. If the remainder is 0 number is not prime.
- No number is divisible by more than half of itself. So, we need to loop through just numberToCheck/2. If the input is 17, half is 8.5, and the loop will iterate through values 2 to 8
- If numberToCheck is entirely divisible by another number, we return false, and loop is broken.
- If numberToCheckis prime, we return true.
- In the main method, check isPrime is TRUE and add to primeNumbersFound String
- Lastly, print the results

```auto
public class primeNumbersFoundber {
 
    public static void main(String[] args) {
 
        int i;
        int num = 0;
        int maxCheck = 100; // maxCheck limit till which you want to find prime numbers
        boolean isPrime = true;
 
        //Empty String
        String primeNumbersFound = "";
 
        //Start loop 1 to maxCheck
        for (i = 1; i <= maxCheck; i++) {
            isPrime = CheckPrime(i);
            if (isPrime) {
                primeNumbersFound = primeNumbersFound + i + " ";
            }
        }
        System.out.println("Prime numbers from 1 to " + maxCheck + " are:");
        // Print prime numbers from 1 to maxCheck
        System.out.println(primeNumbersFound);
    }
    public static boolean CheckPrime(int numberToCheck) {
        int remainder;
        for (int i = 2; i <= numberToCheck / 2; i++) {
            remainder = numberToCheck % i;
            //if remainder is 0 than numberToCheckber is not prime and break loop. Elese continue loop
            if (remainder == 0) {
                return false;
            }
        }
        return true;
 
    }
 
}
```

**Output** :

```
Prime numbers from 1 to 100 are:
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

```

## ENJOY & HAPPY LEARNING! 👍

---

<div class="post-metadata">

**Author:** ![sadatsayed](https://onehack.st/user_avatar/onehack.st/sadatsayed/32/22866_2.png) [@sadatsayed](https://onehack.st/u/sadatsayed)\
**Post date:** [September 16, 2019, 5:26am UTC](https://onehack.st/t/prime-number-from-1-to-100-program-in-java-see-how/57664/3 "2019-09-16T05:26:23Z")

</div>

However if you run this in a competitive programming then it will show you a TLE error  
To avoid that you can use a faster approach which is the **[Sieve of Eratosthenes](https://www.geeksforgeeks.org/java-program-for-sieve-of-eratosthenes/)**
