C program to find the sum of n numbers using for loop

Last updated:9th Aug 2022

In this program. you will take positive integer input from a user for finding the sum of n natural numbers by using for loop in the C program.

To find the sum of the 10 natural numbers, you will add all numbers from 1 to 10. for example. 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10.

Sum of n number by using for loop


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

    printf("Enter value of n for finding sum of n: ");
    scanf("%d", &n);

    for (i = 1; i <= n; i++) {
        sum = sum + i;
    }

    printf("sum of %d natural number is: %d", n,sum);
    return 0;
}

output

Enter value of n for finding sum of n: 10
sum of 10 natural number is: 55

Explanation of sum of n number by using for loop.

  • Take a number from the user and store the number in a variable n.
  • iterate for loop from 1 to n.
  • inside for loop, add the value of i in the sum variable.
  • after the end of the loop print the sum of n values.