While to sum integers : While « Language Basics « C / ANSI-C






While to sum integers



#include <stdio.h>

void main()
{
   long sum = 0L; 
   int i = 1;     /* Indexes through the integers       */
   int count = 10; /* The count of integers to be summed */


   /* Sum the integers from 1 to count */
   while(i <= count)
     sum += i++;

   printf("Total of the first %d numbers is %ld\n", count, sum);
}


           
       








Related examples in the same category

1.a while loop nested in for loopa while loop nested in for loop
2.While loop to print char
3.Use getchar inside while loop