1 for loop with 2 integers instead of if else

I’m trying to devide canvas into P x Q rows and columns,
Into that Canvas I Draw Z Histograms like this (where Z is input variable):

TH1 *h[“Z”];
Char_t *histname = new Char_t[“Z”];
for(Int_t a=1; a<Z+1; a++){
sprintf(histname,“hist%d”,a);
h[a] = new TH1F(histname,histname,100,-3,3);
Later I filled every histogram with other For Loop, but thats not important here…

Now I created two integers P and Q:

Int_t P;
Int_t Q;

Now for certain number of histograms that is determined by input variable Z, this program Divides canvas so it fits good onto screen like this:

if (Z<3)
{
P=1;
Q=2;
}
else if (Z<5)
{
P=2;
Q=2;
}
else if (Z<7)
{
P=2;
Q=3;
}
else if (Z<10)
{
P=3;
Q=3;
}
… //THIS COUNTINUES TILL P=5,Q=5
else
{
cout<<Z<<“too many histograms for canvas”<<endl;
break;
}
Divide->Divide(P,Q);

so, what is your question ?

Question is how to change this IF ELSE Loop with FOR Loop.
This IF Else loop looks really bad, I mean its “lame”…

Doesn’t Divide->DivideSquare(); do what you want?

I’m new to ROOT, so this is 1st time to see DivideSquare command. I’ll try to do something with this command, it is posible that this can help me. Thank You sir.