## 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 (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(); 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.

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!