Write a C Program to find out the sum of first n numbers

By | 02.11.2016

Find out the sum of first n numbers


Write a C Program to find out the sum of first n numbers. Here’s simple Program to find out the sum of first n numbers in C Programming Language.


LOGIC : : 

The program asks the user to enter the number of terms he/she would like to evaluate the sum of, which is stored in variable n. Another variable i is assigned value 1, which is used for counting the number of terms in the loop and also as a natural number which is added to the result.

The variable sum stores sum of the natural numbers. The sum is initialized to 0 to avoid addition of garbage numbers.As the program enters the loop, value of i is added to the variable sum which now equals the sum of all natural numbers till i. Then the value of i is increased by 1.

The loop continues as long as the value of i is less than or equal to n as we are calculating the sum of first n natural numbers. When, the loop exits, the result is displayed on the screen.


Below is the source code for C Program to find out the sum of first n numbers which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C Program to find out the sum of first n numbers */

#include<stdio.h>
#include<conio.h>

int main()
{
  int i=0,num,sum=0;
  printf("Enter a number :: ");
  scanf("%d",&num);
  for(;i<num;i++)
  {
  sum=sum+i;
  }
  printf("\n The sum of numbers = %d",sum);

  return 0;
}

OUTPUT : :


/* C Program to find out the sum of first n numbers */

Enter a number :: 6

 The sum of numbers = 15

Above is the source code for C Program to find out the sum of first n numbers which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

4.8 5 votes
Article Rating
Category: Basic Programs C Programming Number Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Eayaz

sir please check in your website Find out the sum of first n numbers
this c program.

sujan subedi

this is wrong
the correct one loop is
for(,i<=n;i++){