[prev] 44 [next]

Exercise 8: Function to compute 1+2+3+...+n

Implement the function sumTo()

int main()
{
   int max;
   print("Enter +ve integer: ");
   scanf("%d", &max);
   printf("Sum 1..%d = %d\n", max, sumTo(max));
   return 0;
}

int sumTo(int n)
{
   int sum = 0;
   for (int i = 1; i <= n; i++) sum += i;
   return sum;
}