# Program tha calculates the square and cube of the numbers from 1 to 10.

This is a discussion on Program tha calculates the square and cube of the numbers from 1 to 10. within the C Programming forums, part of the General Programming Boards category; / * Program tha calculates the square and cube of the numbers from 1 to 10. This is my code ...

1. ## Program tha calculates the square and cube of the numbers from 1 to 10.

/ * Program tha calculates the square and cube of the numbers from 1 to 10.
This is my code so far, it is instructed to only use the if statement (no else, while, for, etc).
And use the \t to print the bellow table, any hints???? thanks!! */

Code:
```
number   square   cube
0          0        0
1          1        1
2          4        8
3          9        27
4          16       64
5          25       125
6          36       216
7          49       343
8          64       512
9          81       729
10         100     1000

I've achieved so far the following code:

#include <stdio.h>
#include <conio.h>

main()

{

int num, squ, cub;

num = 0;

squ = num * num;

cub = num * num * num;

printf("\nnumber\tsquare\tcube\n\n");

printf("%d\t%d\t%d\n\n", num, squ, cub);

getch();

return 0;
}```
Any suggestions will be appreciated!!!!!!

2. I am a complete newbie, maybe more so than you are but I can tell right off the bat you need an if loop and some kind of increment operator (++) to go along with it in order to print that table as it is shown

3. First off ... you are doing your calculations before you know the values of the variables. The order of execution in a C file is top to bottom... it won't go back and look for it...

Second, I would seriously question why you can't use loops. With a for() loop... it would take about 5 or 6 lines of code...

In fact, the only way to do this without while() do-while() or for() is to use goto() and that is a seriously depricated practice. C has loops for just that reason...

If you can post the original assignment, that would help...

4. Or make a pair of lookup tables which gives all the squares and cubes of 1 through 10.

5. Originally Posted by whiteflags
Or make a pair of lookup tables which gives all the squares and cubes of 1 through 10.
Still needs a loop of some kind to print them...

6. All I can say is, unless you're expected to use the pow function and employ senseless use of code duplication, you've misunderstood your requirements.
Go back and make sure that what you think you're not allowed to use is exactly what you are not allowed to use.

7. Originally Posted by CommonTater
Still needs a loop of some kind to print them...
Not really, but like iMalc says, code duplication will happen.

8. Recursion!

9. Hi guys, I got it!! Thanks for your help and suggestions, here is the code if somebody needs help.

Code:
```#include <stdio.h>
#include <conio.h>

main()

{

int num0, squ0, cub0, num1, squ1, cub1, num2, squ2, cub2, num3, squ3, cub3, num4, squ4, cub4,
num5, squ5, cub5, num6, squ6, cub6, num7, squ7, cub7, num8, squ8, cub8, num9, squ9, cub9,
num10, squ10, cub10;

num0 = 0;
num1 = 1;
num2 = 2;
num3 = 3;
num4 = 4;
num5 = 5;
num6 = 6;
num7 = 7;
num8 = 8;
num9 = 9;
num10 =10;

printf("\nnumber\tsquare\tcube\n\n");

squ0 = num0 * num0;

cub0 = num0 * num0 * num0;

printf("%d\t%d\t%d\n\n", num0, squ0, cub0);

squ1 = num1 * num1;

cub1 = num1 * num1 * num1;

printf("%d\t%d\t%d\n\n", num1, squ1, cub1);

squ2 = num2 * num2;

cub2 = num2 * num2 * num2;

printf("%d\t%d\t%d\n\n", num2, squ2, cub2);

squ3 = num3 * num3;

cub3 = num3 * num3 * num3;

printf("%d\t%d\t%d\n\n", num3, squ3, cub3);

squ4 = num4 * num4;

cub4 = num4 * num4 * num4;

printf("%d\t%d\t%d\n\n", num4, squ4, cub4);

squ5 = num5 * num5;

cub5 = num5 * num5 * num5;

printf("%d\t%d\t%d\n\n", num5, squ5, cub5);

squ6 = num6 * num6;

cub6 = num6 * num6 * num6;

printf("%d\t%d\t%d\n\n", num6, squ6, cub6);

squ7 = num7 * num7;

cub7 = num7 * num7 * num7;

printf("%d\t%d\t%d\n\n", num7, squ7, cub7);

squ8 = num8 * num8;

cub8 = num8 * num8 * num8;

printf("%d\t%d\t%d\n\n", num8, squ8, cub8);

squ9 = num9 * num9;

cub9 = num9 * num9 * num9;

printf("%d\t%d\t%d\n\n", num9, squ9, cub9);

squ10 = num10 * num10;

cub10 = num10 * num10 * num10;

printf("%d\t%d\t%d\n\n", num10, squ10, cub10);

getch();

return 0;
}```
By the way, no if was really neccesary!! Because the number range was limited.

10. You could just do this without all of the separate calculation lines, and a single variable:
Code:
```v = 0
printf("%d\t%d\t%d\n\n", ++v, v*v, v*v*v); /* ten times */```

Quzah.

11. Originally Posted by quzah
You could just do this without all of the separate calculation lines, and a single variable:
Code:
```v = 0
printf("%d\t%d\t%d\n\n", ++v, v*v, v*v*v); /* ten times */```

Quzah.
Isn't the order in which parameters are evaluated unspecified? Doesn't that mean that v*v could happen first, then ++v, then v*v*v, giving incorrect results? Or does ++v create some sequence point that prevents this from happening?

12. I was going to split the increment up onto its own line, but I was thinking I'd be OK on that one. You could be right on that.

Quzah.

13. I just read a thread yesterday how some compilers evaluate the arguments right to left. Must have been on another forum. So yes, you'd need to break it.

Oh and I've liked the recursion option because the funny part is that in order to prevent infinite recursion you'd basically still need an if-statement. Nice try though!

14. Actually, according to the OP, if is the only thing s/he can use. But just for kicks, here's an if-free recursive version:
Code:
```#include <stdio.h>

int powers(int n)
{
return (n < 0) || (powers(n-1) && printf("%d\t%d\t%d\n", n, n*n, n*n*n));
}

int main(void)
{
printf("number\tsquare\tcube\n");
powers(10);

return 0;
}```