Archive
Posts Tagged ‘Java Basic Programs’
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):
Categories: Java
Java Basic Programs, Magic Square
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):
Categories: Java
Calculate Week Day, Java Basic Programs
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):
Categories: Java
Java Basic Programs, Pascal Triangle
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
Java Basic Programs
Java Program to find Armstrong number (sum of cubes of individual digits is equal to the given number) – Q11
January 6, 2009
Leave a comment
Q11: Java Program to find Armstrong number (sum of cubes of individual digits is equal to the given number)
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
For example: 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
class armg{ public static void main(String args[]){ int num; num = Integer.parseInt(args[0]); int res=0, x, n; while(num != 0){ x = num % 10; res += x*x*x; num /= 10; } n = Integer.parseInt(args[0]); if(n == res) System.out.println(args[0] + " is Armgstrong Number"); else System.out.println(args[0] + " is not Armgstrong Number"); } }
… from College notes (BCA/MCA assignments):
Categories: Java
Armstrong number, Java Basic Programs