C Program to Find Sum of N Numbers Using Function

Here we will write the C program to find the sum of n numbers using function. First, we will develop a simple program to find the sum of n numbers using function. Later we will do the same thing by defining three functions.

Prerequisites:-
Introduction to Function in C
User-defined Functions in C

C Program to Find Sum of N Numbers Using Function

#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("Upto which number you want to find sum: ");
   scanf("%d", &range);
   result = sum(range);
   printf("1+2+3+….+%d+%d = %d",range-1, range, result);
}

Output for different test-cases:-

Upto which number you want to find sum: 10
1+2+3+….+9+10 = 55

Upto which number you want to find sum: 15
1+2+3+….+14+15 = 120

In this program, we defined a function sum() which takes one argument. Using for loop, the function sum() finds the sum of series 1+2+…+(n-1)+n; Later this value is returned back to the caller function.

Inside the main function, we asked the user to enter the range up to which you want to find the sum? The entered value from the user is passed to the function sum() as an argument. When compiler encounters result = sum(range); then control goes to function sum() with argument. After completion of the execution of the function, control came back to the main function with some value. The returned value from the function is stored in the variable result. Finally, the result has displayed on the screen.

Using More Than One Function

Program description:- Write a C program to find the sum of n numbers using functions. Define three functions input(), sum(), and display(). Take input from the user in the input() function and return it to the main function. Find the sum of n numbers in the sum() function, return the sum value to the main function. Print result using display() function.

#include<stdio.h>

// function for taking input from user
int input()
{
   int n;
   printf("Up to which number you want to find the sum: ");
   scanf("%d", &n);
   return n;
}

// function to find sum of two numbers
int sum(int num)
{
   int add = 0;
   for(int i=1; i<=num; i++)
   {
     add += i;
   }
   return add;
}

// function for displaying the result
void display(int n, int sum)
{
   printf("1+2+3+….+%d+%d = %d",n-1, n, sum);
}

int main()
{
   int range, result;
   range = input();
   result = sum(range);
   display(range, result);
}

Output for the different test-cases:-

Up to which number you want to find the sum: 5
1+2+3+….+4+5 = 15

Up to which number you want to find the sum: 12
1+2+3+….+11+12 = 78

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Follow Us
Instagram • Twitter • Youtube • Linkedin • Facebook • Pinterest • Telegram

Leave a Comment

Your email address will not be published. Required fields are marked *