Archive

Archive for the ‘Java’ Category

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):


Java Program to find fibonnaci series up to a particular number – Q4

January 2, 2009 Leave a comment

Q4: Java Program to find factorial of a given number without recursion

The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones.

For example:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

class fibseries{
	public static void main(String args[]){
		int a = Integer.parseInt(args[0]);
		int f=0, f1=0, f2=1;
		System.out.print("Fibbonacci Series: ");
		for(int i=0; i<a; i++){
			System.out.print(f1 + " ");
			f = f1 + f2;
			f1 = f2;
			f2 = f;
		}
	}
}

… from College notes (BCA/MCA assignments):


Java Program to find factorial of a given number with recursion – Q3

January 2, 2009 Leave a comment

Q3: Java Program to find factorial of a given number with recursion

The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

For example:
5! =     5*4*3*2*1 = 120
6! = 6*5*4*3*2*1 = 720

class fact_rec{
	public static void main(String args[]){
		int num;
		long fac = 1;
		num = Integer.parseInt(args[0]);
		fac = factorial(num);
		System.out.println("Factorial of " + num + " is: " + fac);
	}
	public static long factorial(int num){
		if(num<=1)
			return 1;
		else
			return (num * factorial(num-1));
	}
}

… from College notes (BCA/MCA assignments):


Java Program to find factorial of a given number without recursion – Q2

January 1, 2009 Leave a comment

Q2: Java Program to find factorial of a given number without recursion

The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

For example:
5! =     5*4*3*2*1 = 120
6! = 6*5*4*3*2*1 = 720

class fact{
	public static void main(String args[]){
		int num;
		long fac = 1;
		num = Integer.parseInt(args[0]);
		for(int i=1; i<=num; i++){
			fac *= i;
		}
		System.out.println("Factorial of " + num + " is: " + fac);
	}
}

… from College notes (BCA/MCA assignments):