Write a Java program that find prime numbers between 1 to n


Write a Java program that find prime numbers between 1 to n             

 Week -1

b) Aim: To Write a Java program that find prime numbers 

between 1 to n.
 
Source Code:
import java.io.*;
public class Prime {
public static void main(String[]args)throws IOException
{
BufferedReader br=new BufferedReader(new
 InputStreamReader(System.in));
System.out.println(“Enter n:”);
int n=Integer.parseInt(br.readLine());
int k=0;
for(int i=1;i<=n;i++)
{
if((i==1)||(i==2))
System.out.println(i);
for(int j=2;j<i;j++)
{
if(i%j==0)
{
k=0;
break;
}
else
k++;
}
if(k>0)
System.out.println(i);
}
}
}
 
Expected Output:
Enter n: 5
2 35

List:

Write a Java program that find prime numbers between 1 to n

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Domain & free Hosting

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.