Home > Java > Java Program to find n prime number – Q7

Java Program to find n prime number – Q7


Q7: Java Program to find n prime number

Prime Number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
For example: 2, 3, 5, 7, 11, 13, 19, 23, 29, etc.

class primeupto{
	public static void main(String args[]){
		boolean pr = false;
		int a = Integer.parseInt(args[0]);
		System.out.print("Prime Numbers upto " + a + " are: " + 2);
		for(int i=3; i<a; i++){
			for(int j=2; j<i; j++){
				if(i % j == 0){
					pr = false;
					break;
				}
				else
					pr = true;
			} // end of for 2
			if(pr==true)
				System.out.print(" " + i);
		} // end of for 1
	} // end of main
} // end of class primeupto

… from College notes (BCA/MCA assignments):


Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: