Sum the integers from 1 to a user-specified number with for Loop - C Statement

C examples for Statement:for

Description

Sum the integers from 1 to a user-specified number with for Loop

Demo Code

#include <stdio.h>

int main(void)
{
  unsigned long long sum = 0LL;            // Stores the sum of the integers
  unsigned int count = 0;                   // The number of integers to be summed

  printf("\nEnter the number of integers you want to sum: ");
  scanf(" %u", &count);

  for(unsigned int i = 1 ; i <= count ; ++i)
    sum += i;//from  ww w .j  a  va 2s.  c o m

  printf("\nTotal of the first %u numbers is %llu\n", count, sum);
  return 0;
}

Result


Related Tutorials