The mentioned C Program to find the sum of natural numbers in a given range. The minimum and maximum value of the range ask by users.
#include<stdio.h>
int main()
{
int rangeMinValue,rangeMaxValue, i;
unsigned long int sum = 0;
printf("Please Enter the rangeMinValue of natural number = ");
scanf("%d",&rangeMinValue);
printf("Please Enter the rangeMaxValue of natural number = ");
scanf("%d",&rangeMaxValue);
if((rangeMinValue > 0) && (rangeMaxValue > 0) && (rangeMaxValue > rangeMinValue ))
{
printf("List of Natural Numbers from %d to %d are \n",rangeMinValue,rangeMaxValue);
for(i = rangeMinValue; i <= rangeMaxValue; i++)
{
sum += i;
}
printf("Sum = %ld\n",sum);
}
else
{
printf("Enter Valid numbers\n");
}
return 0;
}
Output:
Please Enter the rangeMinValue of natural number = 1 Please Enter the rangeMaxValue of natural number = 10 List of Natural Numbers from 1 to 10 are Sum = 55
The mentioned C Program to find the sum of natural numbers in a given range. The minimum and maximum value of the range ask by users.
Output:
Please Enter the rangeMinValue of natural number = 1
need an explanation for this answer? contact us directly to get an explanation for this answerPlease Enter the rangeMaxValue of natural number = 10
List of Natural Numbers from 1 to 10 are
Sum = 55