The number which is only divisible by itself and 1 is known as prime number. For example 2, 3, 5, 7…are prime numbers. Here we will see two programs: 1) First program will print the prime numbers between 1 and 100 2) Second program takes the value of n (entered by user) and prints the prime numbers between 1 and n. If you are looking for a program that checks whether the entered number is prime or not then see: Java Program to check prime number.
Program to display the prime numbers from 1 to 100
It will display the prime numbers between 1 and 100.
class PrimeNumbers { public static void main (String[] args) { int i =0; int num =0; //Empty String String primeNumbers = ""; for (i = 1; i <= 100; i++) { int counter=0; for(num =i; num>=1; num--) { if(i%num==0) { counter = counter + 1; } } if (counter ==2) { //Appended the Prime number to the String primeNumbers = primeNumbers + i + " "; } } System.out.println("Prime numbers from 1 to 100 are :"); System.out.println(primeNumbers); } }
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
Program to display prime numbers from 1 to n
It will display all the prime numbers between 1 and n (n is the number, entered by user).
import java.util.Scanner; class PrimeNumbers2 { public static void main (String[] args) { Scanner scanner = new Scanner(System.in); int i =0; int num =0; //Empty String String primeNumbers = ""; System.out.println("Enter the value of n:"); int n = scanner.nextInt(); scanner.close(); for (i = 1; i <= n; i++) { int counter=0; for(num =i; num>=1; num--) { if(i%num==0) { counter = counter + 1; } } if (counter ==2) { //Appended the Prime number to the String primeNumbers = primeNumbers + i + " "; } } System.out.println("Prime numbers from 1 to n are :"); System.out.println(primeNumbers); } }
Output:
Enter the value of n: 150 Prime numbers from 1 to n 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 101 103 107 109 113 127 131 137 139 149
Saleema says
can you explain in details of the 1st program. I can’t seem to quite follow your logic. Thanks in advance.
Subba Reddy Mettu says
The person who wrote the logic has written a very good code, I am not expert in java and this would be my first explanation hope you will like it.
int n = scanner.nextInt() – It takes the input from the user to find the prime numbers in a particular range say 100.
for (i = 1; i =1; num–) – for (1=i; 1>=1; 1–) 1– is 0 next step is if(i%num==0) – 1%1 ==0 yes counter = 0 + 1 which is 1 now num is 0 so num>=1 which is 0>=1 false come out of loop.
so, the counter is just 1, if the counter is exactly equal to two that means if the numbers have exactly two factors which is one and itself the the number is updated to prime number and is printed.
lets see true condition: say number is 5
for(num =i; num>=1; num–) – for (5=i; 5>=1; 5–) 5– is 4 next step is if(i%num==0) – 5%5 ==0 yes counter = 0 + 1 which is 1 now num is 4 so num>=1 which is 4>=1 true execute the loop again.
for(num=i;num>=1;num–) – for(4=i; 4>=1; 4–) 4– i%num for 5%4 is 1 and 5%3 is 2 for 5%2 its 1 and for 5%1 its zero, so increase counter by 1 so the counter is exactly equal to 2 so 5 is a prime number and it printed at end.
gangadhar says
will you explain this
primeNumbers = primeNumbers + i + ” “;
Manjunath says
look at this declaration
String primeNumbers = “”;
primeNumbers is declared as of type String so,
whenever new prime number is found its been appended to the same string with white space in between. (would have used array rather)
primeNumbers = primeNumbers + i + ” “;
A.S Sahshransu says
The Best Ever Writer Of the Program Is Thanks receiver From Me
I am grateful to you
I was able to do my Projects Well
Thank You Very Much Beginner’s Book
I Shall Visit Again For Knowledge
The Great site Of Knowledge I have Ever seen
sivaiah says
Very nice logic…Thanks
Chaitanya Singh says
Glad to know that you liked it.
Anonymous says
Hey,I just went through your code seems very good,I need your help with some java codes please?
sai.g says
Good logic very nice
Akansh says
I was stuck for 2 day thinking about the logic.Thanks I got it here good and simple.
Manav says
Can you explain please?
venkat says
i need too easy one
SaitamaJBP says
class Number3
{
public static void main (String[] args)
{
for(int x = 1 ; x 1)
System.out.print(x+” “);
}
}
}
//this Program Will Show All The prime numbers from 1-100
Sorry my logic is not too deep but i display it right ?
Can i get some opinion ?
Qamran Rajput says
Very Nice Simple Short and easy solution.
Thankyou
shashank siriga says
hey we can use stringbuffer instead of string which may reduce the space in the sotrage for the programm
Pele says
How do you make a space between Prime Number when you print out?
can you tell me?
Ankit Bhardwaj says
primeNumbers = primeNumbers + i + ” “;
if you see this you would see a space between double quotes. This basically add the space . if you increase it the space between the number will increase .
Shubham kankal says
we can not display directly prime numbers……i mean empty string in mandatory…..???
Ankit Bhardwaj says
yes you can that but this empty string is just used to add space between two numbers.
Will Rodriguez says
For those of you wanting an explanation on whats going on in here, here it goes!
He is reading a number from standard input: (Console)
Then you are looping from 1 up to the n provided, i.e n=5, 1,2,3,4,5.
Then you’re trying to only append the primes to the string.
Recall a prime number, are those numbers that can only be divided by 1 and itself.
He then starts looping through the current number, until he reaches 1. Counting the number of times the current number was evenly divided.
for example, current number is 5, then he loops going back from 5 until 1.
5,4,3,2,1. During each iteration, he then checks to see if the current number, in this example 5, is evenly divisible by any other number (i%number ==0)
5%5==0, true, increment counter
5%4==0, false,
5%3==0, false,
5%2==0, false
5%1==0, true, increment counter
At this point, only 2 numbers evenly divided 5, therefore, 5 is prime!
We now append the current number to our string.
Hope this helped!!
Oh and for that person that asked if it’s better to use a stringbuffer, well yes! Strings are immutable, therefore, every time you are appending a value to it, you are creating a new string object!
Jospin Okamba says
what i want my output start from let say 3 instead of 2, what change should should i make on the second project or code u wrote
ashish maheshwari says
very well explained,i m stuck in one part,say i am using scanner to take entry from user,now if i want this scanner input entry from user again n again without re running the program how can i do.
please suggest
thanks
Chaitanya Singh says
You can place the scanner in a while loop and inside loop along with scanner, ask user whether he/she wants to continue(like Y/N, Y for yes and N for no), run the loop until that new variable is equal to N.