Complete IIT AIEEE ICSE ISC PMT help forum
This is a free help Forum for Indian Students.
Complete IIT AIEEE ICSE ISC PMT help forum

Complete help forum for students preparing for icse iit aieee cbse pmt and other related exams.Stay updated about all 2011 exams.
 
HomeHome  PortalPortal  FAQFAQ  SearchSearch  RegisterRegister  MemberlistMemberlist  UsergroupsUsergroups  Log in  India NewsIndia News  
Keywords
CLASS 2010 RESULT YEAR
Latest topics
Top posters
abhas
 
Anu..i luv icse...:)
 
G-7
 
siya
 
Candy
 
jOhNy
 
Apurva
 
WinRrule
 
ambili
 
saif
 



Search if you can't find it here

Magic Square(odd order)

View previous topic View next topic Go down
AuthorMessage
sa12
Active member
Active member



PostSubject: 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
Back to top Go down
View user profile
sa12
Active member
Active member



PostSubject: 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();
}

}
}
Back to top Go down
View user profile
saif
Active member
Active member



PostSubject: 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)
Back to top Go down
View user profile
sa12
Active member
Active member



PostSubject: 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. Smile 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);
}
}
}
}
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: Magic Square(odd order)

Back to top Go down

Magic Square(odd order)

View previous topic View next topic Back to top
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Complete IIT AIEEE ICSE ISC PMT help forum :: ISC :: ISC 2010 [Class xii] :: Computer-