Prime number between 1 to 100 in java
Do you know how we can print all prime numbers between 1 and 100 using java ? Don't worry I will simply explain how to write a program for that.
Here I have used my android phone to write code. The java ide i.e java N - IDE is used. Please support.
Approach:-1 "print prime numbers from 1 to 100 in java"
public class ptintBetween {
public static void main(String[] args){
int temp = 0;
for(int i = 2; i <= 100; i++)
{
for(int j = 2; j < i; j++)
{
if(i % j == 0)
{
temp = temp + 1;
}
}
if(temp == 0){
System.out.print(i + ", ");
}
else{
temp = 0;
}
}
}
}
I know you can easily check whether a number is prime or not, but printing all prime numbers between two interval is little more confusing.
Here we have to use two for loop, one is to access all numbers from 1 to 100. And inside another for loop, we will check the condition regarding prime number.
Initially I have declared a variable i.e temp, now if the condition for not prime is satisfied I have incremented the value of temp with 1.
Now outside of the 2nd for loop, I have checked, wheather the value of temp is 0 or something else. If the value of temp is 0, then that particular number is prime, I have printed it.
Now all the numbers between 1 to 100 will be checked one by one, and for those numbers the temp value remain 0 will be printed. And these are the list of all prime numbers.
Output
If you are getting any doubt or if you have any questions then please ask me in the comment below, I will definitely answer to your questions.