C Program To Find Sum of N Numbers

In this article, we are going to write a c program to find the sum of n numbers.

We will make this program in the following way -:

  • C Program to Find Sum of n Numbers Using For Loop
  • C Program to Find the Sum of n Numbers Using While Loop
  • C Program to Find the Sum of n Numbers Using Do While Loop
  • C Program to Find the Sum of n Numbers Using Function

Let’s understand all these methods one by one.

C Program to Find Sum of n Numbers 

C Program To Find Sum of N Numbers Using For Loop 

To make this program, you must first know about the loop because we are going to use the concept of loop to make this program.

There are mainly three types of loops – : While Loop, Do While Loop and For Loop.

You can read about loop in detail here -:

Here we are going to make this program with the help of these three types of loops.

Let’s first make this program with the help of simple loop (For Loop)

Program -:

//Program to Calculate Sum of n Numbers In C Using Loop

#include<stdio.h>
int main()
{
  int i,n,sum=0,num;
  float avg;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  printf("\nEnter elements one by one\n");
  for(i=0;i<n;++i)
   {
     scanf("%d",&num);
     sum = sum +num;
   }

  printf("\nSum of given Numbers = %d",sum);

  return 0;
}

Output -:

Enter How Mant Number You Want?
5
Enter elements one by one 
10
20
30
40 
50

Sum of given number = 150

C Program to Find the Sum of n Numbers Using While Loop

Program -:

//C Program to Find the Sum of n Numbers Using While Loop

#include<stdio.h>
int main()
{
  int i = 0, n, sum=0, num;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  printf("\nEnter elements one by one\n");
  while(i<n)
   {
     scanf("%d",&num);
     sum = sum +num;
     i++;
   }

  printf("\nSum of given Numbers : %d",sum);

  return 0;
}

Output -:

Enter How Mant Number You Want?
5
Enter elements one by one 
1
2
3
4 
5

Sum of given number : 15

C Program to Find the Sum of n Numbers Using Do While Loop

Program -:

//C Program to Find the Sum of n Numbers Using Do While Loop

#include<stdio.h>
int main()
{
  int i = 0, n, sum=0, num;
  float avg;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  printf("\nEnter elements one by one\n");
  do
   {
     scanf("%d",&num);
     sum = sum +num;
     i++;
   }while(i<n);

  printf("\nSum of Given Numbers : %d",sum);

  return 0;
}

Output -:

Enter How Mant Number You Want?
3
Enter elements one by one 
1
2
3

Sum of given number : 6

C Program to Find the Sum of n Numbers Using Function

To make and understand this program made by function, first of all you need to know the basic of the function.

You can read about the function from here -: Function in C

Program -:

//C Program to Find the Sum of n Numbers Using Function

#include<stdio.h>
int sum(int);
int main()
{
  int n, s;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  //calling function to calculate sum
   s = sum(n);

  printf("\nSum of given Numbers = %d", s);

  return 0;
}
int sum(int n)
{
  int i, sum=0, num;

  printf("\nEnter elements one by one\n");
  for(i=0;i<n;i++)
   {
     scanf("%d",&num);
     sum = sum +num;
   }
   return (sum);
}

Output -:

Enter How Mant Number You Want?
3
Enter elements one by one 
11
22
33

Sum of given number : 66

Conclusion

So friends, in this article, we have write a c program to calculate the sum n numbers in five different ways.

  • C Program to Find the Sum of n Numbers Using For Loop 
  • C Program to Find the Sum of n Numbers Using While Loop
  • C Program to Find the Sum of n Numbers Using Do While Loop
  • C Program to Find the Sum of n Numbers Using Function

If you have difficulty to understand any program or have any doubts or questions, then tell me in the comment below.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment