Q:

C Program to find the sum of natural numbers within a range

belongs to collection: C Programming on Numbers

0

C Program to find the sum of natural numbers within a range

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Programming on Numbers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to find the sum of odd natural numbers f... >>
<< C Program to find the sum of natural numbers upto ...