Mega Code Archive

 
Categories / C / Language Basics
 

Nested for

#include <stdio.h> void main() {    long sum = 0L;     int i = 1;     /* Outer loop control variable     */    int j = 1;     /* Inner loop control variable     */    int count = 10; /* Number of sums to be calculated */       for( i = 1 ; i <= count ; i++ )    {      sum = 0L;  /* Initialize sum for the inner loop */      /* Calculate sum of integers from 1 to i */      for(j = 1 ; j <= i ; j++ )        sum += j;        printf("\n%d\t%ld", i, sum); /* Output sum of 1 to i */    } }