Problem:
Write a c program to find the sum of first n natural numbers.
Solution:
Copy the below c program and execute it to see the real time output. We have used the for loops to write c program.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h> int main() { int n = 4; int sum=0, s=0; for(int i = 1; i< n; i++){ for(int j= 1; j<i;j++ ){ s+= j; } sum += s; } printf("the sum of sum of natural number till %d is %d", n,sum); return 0; } |
Output:
1 | The sum of sum of natural number till 4 is 5 |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.