Example
Input
Input upper limit: 10
Output
Sum of natural numbers 1-10: 55
/**
* C program to find sum of natural numbers between 1 to n
*/
#include <stdio.h>
int main()
{
int i, n, sum=0;
/* Input upper limit from user */
printf("Enter upper limit: ");
scanf("%d", &n);
/* Find sum of all numbers */
for(i=1; i<=n; i++)
{
sum += i;
}
printf("Sum of first %d natural numbers = %d", n, sum);
return 0;
}
/*
Output :
Enter upper limit: 10
Sum of first 10 natural numbers = 55
*/
Program to find sum of natural numbers in given range
/**
* C program to find sum of natural numbers in given range
*/
#include <stdio.h>
int main()
{
int i, start, end, sum=0;
/* Input lower and upper limit from user */
printf("Enter lower limit: ");
scanf("%d", &start);
printf("Enter upper limit: ");
scanf("%d", &end);
/* Find sum of all numbers */
for(i=start; i<=end; i++)
{
sum += i;
}
printf("Sum of natural numbers from %d to %d = %d", start, end, sum);
return 0;
}
/*
Output :
Enter lower limit: 10
Enter upper limit: 15
Sum of natural numbers from 10 to 15 = 75
*/