Page 429 - Computer science 868 Class 12
P. 429
col=col-1;
}
else{}
a[row][col]=n; // store number
generate(row-1,col+1,n+1); // recursive call
}}
void print()
{ generate(0,size/2,1);
System.out.println("Magic square is of size "+size+" is"); // display matrix
for(int i=0;i<size;i++)
{ for(int j=0;j<size;j++)
{ System.out.print(a[i][j]+"\t");}
System.out.println();
}
}
public static void main(int s)
{ if(s%2==0) // if size is even
{ System.out.println("Size is even.. Magic square not generated");}
else
{ Magicsq ob=new Magicsq(s);
ob.print();
}
}}
4. A class Roman is defined to convert any positive integer <=4000 to its Roman equivalent.
The details of the class is given below.
Class name : Roman
Data Members
String rom : To store roman number
int n : To store any number
int v[] : To store integer equivalent of different place values in roman number
system.
String r[] : To store roman letters of different place values
Member Methods
Roman() : Initialise n to 0 and rom to null
void accept() : To accept any number n
String convert(int num, int i) : Using recursive technique, convert the number to its roman form
where num is the number and i its index
void display() : Calls convert(int,int) and displays the number and its roman value
static void main() : To create object and call other methods
Ans. import java.util.*;
class Roman
{ //variable declaration
int n;
String rom;
String r[]={"IXM","VM","IVM","M","CM","D","CD","C",
"XC","L","XL","X","IX","V","IV","I"};
int v[]={9000,5000,4000,1000,900,500,400,100,90,
427
Recursion 427

