Advertisements

Archive

Archive for January 4, 2009

Java Program to add two matrices – Q8

January 4, 2009 Leave a comment

Q8: Java Program to add two matrices

… from College notes (BCA/MCA assignments):

class matrix_add{
	public static void main(String args[]){
		int mat1[][] = { {1,2,3},
						 {4,5,6},
						 {7,8,9} };
		int mat2[][] = { {9,8,7},
						 {6,5,4},
						 {3,2,1} };
		
		int tot[][] = new int[3][3];
		int i, j;
		
		for(i=0; i<3; i++){
			for(j=0; j<3; j++){
				tot[i][j] = mat1[i][j] + mat2[i][j];
			}
		}
		for(i=0; i<3; i++){
			for(j=0; j<3; j++){
				System.out.print(" " + tot[i][j]);
			}
			System.out.println();
		}
	}
}

Advertisements

Java Program to find n prime number – Q7

January 4, 2009 Leave a comment

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