a while loop nested in for loop : While « Language Basics « C / ANSI-C






a while loop nested in for loop

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 */
   }
}


           
       








Related examples in the same category

1.While to sum integers
2.While loop to print char
3.Use getchar inside while loop