Loop
-
I was asked to write the multiply table in c, output will be something like : 1 2 3 4 5 6 7 2 4 6 8 .. ....etc for 12, so I did it like this
for (a=1;a<=12;a++)
{
for (b=1;b<=12;b++)
printf("%d",a*b);
printf("\n");
}That works, but I was wondering if there exeist another solution to the same problem using only ONE loop ?
-
I was asked to write the multiply table in c, output will be something like : 1 2 3 4 5 6 7 2 4 6 8 .. ....etc for 12, so I did it like this
for (a=1;a<=12;a++)
{
for (b=1;b<=12;b++)
printf("%d",a*b);
printf("\n");
}That works, but I was wondering if there exeist another solution to the same problem using only ONE loop ?
Quickly looking at this the closed solution formula is f(n) = 2^(n-1) so the loop would be
for (a=1;a<=12;a++) { printf(2^(a-1)); }
Grady Booch: I told Google to their face...what you need is some serious adult supervision. (2007 Turing lecture) http://www.frankkerrigan.com/[^]
F