Archive

Archive for January 8, 2009

Java Program to convert decimal to binary number – Q16

January 8, 2009 Leave a comment

Q16: Java Program to convert decimal to binary number

import javax.swing.*;

class dec_bin{
	public static void main(String args[]){
		String inp, bin;
		int dec, x;
		
		inp = JOptionPane.showInputDialog("Enter a Decimal 
    Number:");
		dec = Integer.parseInt(inp);
		
		bin = "";
		while(dec != 0){
			x = dec % 2;
			bin  = x + bin;
			dec /= 2;
		}
		System.out.println("Binary: " + bin);
	}
}

… from College notes (BCA/MCA assignments):


Advertisement

Java Program to print MAGIC Square – Q15

January 8, 2009 Leave a comment

Q15: Java Program to print MAGIC Square

class magicSq{
	public static void main(String args[]){
		int l, i, j, x, y, val=1;
		
		l = Integer.parseInt(args[0]);
		int mat[][] = new int[l][l];
		
		x = 0;
		y = l/2;
		
		mat[x][y] = val++;
		while(val <= l*l){
			
			x--;
			y--;
			if((x < 0) && (y < 0)){
				x = 1;
				y = 0;
			}
			if(x < 0)
				x = l-1;
			if(y < 0)
				y = l-1;
			
			if(mat[x][y] != 0){
				x+=2;
				y++;
			}
			mat[x][y] = val++;
			System.out.println(x + " " + y + " " + mat[x][y]);
		}
		
		for(i=0; i<l; i++){
			for(j=0; j<l; j++){
				System.out.print("  " + mat[i][j]);
			}
			System.out.println();
		}
	}
}

… from College notes (BCA/MCA assignments):