Sums of successive integer sequences - C Statement

C examples for Statement:while

Description

Sums of successive integer sequences

Demo Code

#include <stdio.h>

int main(void){

  unsigned long sum = 0UL;             // Stores the sum of integers
  unsigned int count = 0;              // Number of sums to be calculated

  // Prompt for, and read the input count
  printf("\nEnter the number of integers you want to sum: ");
  scanf(" %u", &count);

  for(unsigned int i = 1 ; i <= count ; ++i){
    sum = 0UL;                         // Initialize sum for the inner loop

    // Calculate sum of integers from 1 to i
    for(unsigned int j = 1 ; j <= i ; ++j)
      sum += j;/* w  ww .j  av a 2  s.  co  m*/

    printf("\n%u\t%5lu", i, sum);      // Output sum of 1 to i
  }
  printf("\n");
  return 0;
}

Result


Related Tutorials