sa12 Active member
| Subject: Magic Square(odd order) Wed Feb 24, 2010 2:43 am | |
| Magic Square odd order:[code]import java.io.*; public class MagicSquare { public static void main(String args[]) throws IOException{ BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter (odd) size of Magic Square:"); int n=Integer.parseInt(in.readLine()); int a[][]= new int[n][n]; int i=0,j= ((n+1)/2)-1 ; a[i][j]=1; int counter=1; while(counter< n*n){ int itemp=i, jtemp=j; if (itemp==0) itemp=n-1; else itemp--; if (jtemp==n-1) jtemp=0; else jtemp++; if (a[itemp][jtemp]!=0) { itemp=(itemp+2)%n ; jtemp=j;} a[itemp][jtemp]=++counter; i=itemp; j=jtemp; } for(int y=0; y |
|
sa12 Active member
| Subject: Re: Magic Square(odd order) Wed Feb 24, 2010 2:44 am | |
| I don't know why posts are cut but aftr y=0 for(int y=0; y<n; y++){ for(int z=0; z<n ;z++){ System.out.printf("%2d",a[y][z]); System.out.print(" "); } System.out.println(); }
} } |
|
saif Active member
| Subject: Re: Magic Square(odd order) Wed Feb 24, 2010 3:02 am | |
| import java.io.*; public class MagicSquare { public static void main(String args[]) throws IOException{ BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter (odd) size of Magic Square:"); int n=Integer.parseInt(in.readLine()); int a[][]= new int[n][n]; int i=0,j= ((n+1)/2)-1 ; a[i][j]=1; int counter=1; while(counter< n*n){ int itemp=i, jtemp=j; if (itemp==0) itemp=n-1; else itemp--; if (jtemp==n-1) jtemp=0; else jtemp++; if (a[itemp][jtemp]!=0) { itemp=(itemp+2)%n ; jtemp=j;} a[itemp][jtemp]=++counter; i=itemp; j=jtemp; } for(int y=0; y<n; y++){ for(int z=0; z<n ;z++){ System.out.printf("%2d",a[y][z]); System.out.print(" "); } System.out.println(); }
} } joined ur post WOW nice to see that i am not the only one who is awake till 1:30 today Best of luck I just made a prog. to generate truth table for three variable expression of type a.b+b.c+c.a+a.b.c+ ..... (single variable not allowed in expression)(input should be in ablove format only) |
|
sa12 Active member
| Subject: Re: Magic Square(odd order) Wed Feb 24, 2010 3:06 am | |
| I just started studying at 12:20. Didn't want to get into this situation but got into it nonethe less. Note the System.out.printf in my program. The matrix that is printed is properly formatted.Anagrams via Recursion (though this will not come):</br>import java.io.*; public class Anagrams { public static void main(String args[])throws IOException { BufferedReader in= new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a word:"); String word=in.readLine(); System.out.println("The anagrams of the word are:"); printAnagrams("",word); } public static void printAnagrams(String prefix,String word){ if(word.length()<=1) System.out.println(prefix+word); else { for (int i=0;i<word.length();i++){ String current=word.substring(i,i+1); String before=word.substring(0,i); String after=word.substring(i+1); printAnagrams(prefix+current,before+after); } } } } |
|
Sponsored content
| Subject: Re: Magic Square(odd order) | |
|
|
|