Home C Programming Language / Programs / C program to find the sum of first n natural numbers.
🚀 Programming Example

C program to find the sum of first n natural numbers.

👁 5,855 Views
💻 Practical Program
📘 Step Learning
C program to find the sum of first n natural numbers: 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

💻 Program Code

#include<stdio.h>
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;
}
                        

🖥 Program 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.
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.