Nested for loops have many applications, particularly, in programs dealing with sorting of lists, input/output of multi-dimensional arrays, etc. and also in the evaluation of expressions involving more than one parameter. The code for nested/or loops is given below.
int i, j, k ;
for(i = 0; i<= m; i++)
for(j = 0; j <= n; j++)
for(k = 0 ; k <= p; k++)
{Statements;
———-;}
The following program makes use of the nested/or loop to evaluate ex for different integral values of x (x = 1 to x = 4) by summing the first 10 terms of the following series for each value of x:
In the evaluation there would be two loops: In the inner loop, we calculate the value of the series for a particular value of x, i.e., the first 10 terms is summed up for a value of x. This calculation is repeated for different values of x from x = 0 to x = 4. Program is an example of the required program.
Illustrates evaluation of ex for integer powers of x from 0 to 4.
#include <stdio.h> void main() { int n = 10,x ,m; float Sum, Term; clrscr(); for(x =0; x<5; x++) { Term = 1; Sum = Term; for( m = 1; m<=n; m++) { Term = Term *x/m; Sum= Sum+ Term; } printf("e to power %d = %f\n", x, Sum); } }
The output is given below.