C program to print four powers of numbers 1 to 9 using nested for loop

CServer Side ProgrammingProgramming

Nested loops consist of one loop placed inside another loop.

An example of a nested for loop is as follows −

for (initialization; condition; operation){
   for (initialization; condition; operation){
      statement;
   }
   statement;
}

In this example, the inner loop runs through its full range of iterations for each single iteration of the outer loop.

Example

Following is the C program to print the table of first four powers of numbers 1 to 9 by using nested for loop

 Live Demo

#include <stdio.h>
void main(){
   int i, j, k, temp,I=1;
   printf("I\tI^2\tI^3\tI^4 \n");
   printf("--------------------------------\n");
   for ( i = 1; i < 10; i ++) /* Outer loop */{
      for (j = 1; j < 5; j ++) /* 1st level of nesting */{
         temp = 1;
         for(k = 0; k < j; k ++)
            temp = temp * I;
         printf ("%d\t", temp);
      }
      printf ("\n");
      I++;
   }
}

Output

When the above program is executed, it produces the following result −

I    I^2   I^3  I^4
-----------------------
1    1    1     1
2    4    8    16
3    9   27    81
4   16   64   256
5   25  125   625
6   36  216   1296
7   49  343  2401
8   64  512  4096
9   81  729  6561
raja
Updated on 01-Sep-2021 12:52:49

Advertisements