Home > Net >  How to make this a nested For Loop?
How to make this a nested For Loop?

Time:12-22

I made a code wherein I should print the square and cube of the first 10 counting numbers but I used for loops. What I'm looking for is how to print the same output but using nested (for) statement.

Here is my code:

#include<stdio.h>
int main()
{
    int x;
    printf("x\tx*x\tx*x*x\t\n");

    for(x=1; x<=10; x  )
        printf("%d\t%d\t%d\n", x, x*x, x*x*x);
    return 0;
}

CodePudding user response:

With these few items to print, a nested for loop seems unecessary, but if you really want one, it could look like this:

    for(x = 1; x <= 10; x  ) {
        for(int i = 0, X = x; i < 3;   i, X *= x) {
            printf("%d\t", X);
        }
        putchar('\n');
    }

code

  • Related