Archive

Archive for January, 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):


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


Java Program to find the Week Day of given date – Q14

January 7, 2009 Leave a comment

Q14: Java Program to find the Week Day of given date

import javax.swing.*;

class cal{
	public static void main(String args[]){
		int dd, mm, yy, t_y, odd=0;
		String inp;
		
		inp = JOptionPane.showInputDialog("Enter a Day (dd): ");
		dd = Integer.parseInt(inp);
		
		inp = JOptionPane.showInputDialog("Enter a Day (mm): ");
		mm = Integer.parseInt(inp);
		
		inp = JOptionPane.showInputDialog("Enter a Day (yy): ");
		yy = Integer.parseInt(inp);
		
		if( ((dd <= 0) || (dd >= 32)) &&
			((mm <= 0) || (mm >= 13)) &&
			(yy <= 0) ){
			System.out.println("Enter a Valid date.");
			System.exit(0);
			}
		
		t_y = (yy-1) % 400;
		
		if(t_y == 0)
			odd = 0;
		if(t_y >= 300 && t_y < 400){
			odd = 1;
			t_y -= 300;
			}
		if(t_y >= 200 && t_y < 300){
			odd = 3;
			t_y -= 200;
			}
		if(t_y >= 100 && t_y < 200){
			odd = 5;
			t_y -= 100;
			}
		
		odd += t_y/2;
		t_y -= t_y/4;
		odd += t_y;
		
		for(int i=0; i<mm-1; i++)
			odd += DaysThisMonth(i, yy);
		
		odd += dd;
		
		odd = odd % 7;
		
		System.out.println("\n Day on Date " + dd + "/" + mm + "/" 
     + yy + " is: " + week[odd]);
	}
	
	public static String week[] = { "Sunday", "Monday", "Tuesday", 
  "Wednesday", "Thursday",
  "Friday", "Saturday" };
	
	public static int months[] = { 31, 29, 31, 30, 31, 30, 31,
							 31, 30, 31, 30, 31 };
	
	public static int DaysThisMonth(int m, int y){
		if (m != 1)
			return months[m];
		
		if ((y % 4) != 0)		// Not leap year
			return 28;
		if ((y % 100) != 0)		// It is leap year
			return 29;
		if ((y % 400) != 0)		// Not leap year
			return 28;
		return 29;			// It is leap year
	}
}

… from College notes (BCA/MCA assignments):


Java Program to print Pascal Triangle – Q13

January 7, 2009 Leave a comment

Q13: Java Program to print Pascal Triangle

import javax.swing.*;

class pascal{
	public static void main(String args[]){
		int lim, n, r, item, l;
		double x;
		String inp;
		
		inp = JOptionPane.showInputDialog("Enter Limit: ");
		lim = Integer.parseInt(inp);
		
		// For 1st Line
		for(l=0; l<=lim; l++)
			System.out.print("  ");
		System.out.println(" 1");
		
		//For rest Lines
		for(n=2; n<=lim; n++){
			for(l=n; l<=lim; l++){	// Loop for blank Pyramid
				System.out.print("  ");
			}
			for(r=0; r<=n; r++){	// Loop for Pascal Triangle
				x = fact(n)/(fact(r)*fact(n-r));
				item = (int)x;
				System.out.print("  " +  item);
			}
			System.out.println("");
		}
	}
	
	public static long fact(int a){
		if(a <= 0)
			return (1);
		else
			return (a * fact(a-1));
	}
}

… from College notes (BCA/MCA assignments):


Java Program to create star triangle pattern – Q12

January 6, 2009 Leave a comment

Q12: Java Program to create star triangle pattern

class star_triangle{
	public static void main(String args[]){
		int i, j, k;
		for(i=0; i<10; i++){
			for(j=i; j<10; j++){
				System.out.print(" ");
			}
			for(k=0; k<i;  k++){
				System.out.print(" " + "*");
			}
			System.out.println();
		}
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags: