## 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?

Shubham kankal says

we can not display directly prime numbers……i mean empty string in mandatory…..???