Mega Code Archive

 
Categories / C / Language Basics
 

A while loop nested in for loop

#include <stdio.h> void main() {    long sum = 1L;     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 = 1L; /* Initialize sum for the inner loop */      j=1;      /* Initialize integer to be added    */      printf("\n1");      /* Calculate sum of integers from 1 to i */      while(j < i)      {        sum += ++j;        printf("+%d", j); /* Output +j on the same line */      }      printf(" = %ld\n", sum); /* Output  = sum */    } }