- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to print four powers of numbers 1 to 9 using nested for loop
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 −
#include <stdio.h> void main(){ int i, j, k, temp,I=1; printf("I\tI^2\tI^3\tI^4
"); printf("--------------------------------
"); 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 ("
"); 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
Advertisements