Archive

Archive for January 3, 2009

Java Program to find prime numbers up to a given number n – Q6

January 3, 2009 Leave a comment

Q6: Java Program to find prime numbers up to a given number n

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 primetill{
	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, x=0; x<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);
				x++;
			}
		} // end of for 1
	} // end of main
} // end of class primetill

… from College notes (BCA/MCA assignments):


Java Program to find weather a number is prime or not – Q5

January 3, 2009 Leave a comment

Q5: Java Program to find weather a number is prime or not

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 primeornot{
	public static void main(String args[]){
		boolean pr = false;
		int a = Integer.parseInt(args[0]);
		for(int i=2; i<a; i++){
			if(a % i == 0){
				pr = false;
				break;
			}
			else
				pr = true;
		}
		if(pr==true)
			System.out.println(a + " is Prime.");
		else
			System.out.println(a + " is not Prime.");
	}
}

… from College notes (BCA/MCA assignments):