C program to find the sum of first n natural numbers.
C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)
5666Program:
#include int main() { int i,n,s=0; printf("Enter value of n:"); scanf("%d",&n); for(i=1;i<=n;i++) { s=s+i; } printf("Sum = %d \n",s); return 0; }
Output:
Enter value of n:10 Sum = 55 Press any key to continue . . .
Explanation:
This program prints the sum of first n natural numbers. A number is asked from the user and stored in variable n. For loop is used to add numbers from 1 to n and store the sum in s. Variable i is used for looping and it is incremented on each iteration. The condition is checked and until i is less than or equal to n, the loop runs. Finally the value of sum is printed after terminating the loop.
This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.