Home » C Program to Find Sum of N Numbers Using Function

C Program to Find Sum of N Numbers Using Function

Learn about C Program to Find Sum of N Numbers Using Function in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

C Program to Find Sum of N Numbers Using Function

Program:

#include<stdio.h>

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

int main()
{
   int range, result;
   printf("Number you want to find sum: ");
   scanf("%d", &range);
   result = sum(range);
   printf("1+2+3+….+%d+%d = %d",range-1, range, result);
}

Output:

Number you want to find sum: 5
1+2+3+….+4+5 = 15

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Addition of Two Numbers Using Three Functions
C Program for Addition of Two Numbers Using Functions