





Please login
Prime

Prepinsta Prime
Video courses for company/skill based Preparation
(Check all courses)
Get Prime Video
Prime

Prepinsta Prime
Purchase mock tests for company/skill building
(Check all mocks)
Get Prime mock
Program to find the sum of numbers in a given range in C++
Program to find the Sum of Numbers in a given range
Here we will discuss how to find the Sum of Natural Numbers in a range defined by user using c++ programming language.
All the positive integers (1, 2, 3, 4…) are the Natural Numbers.
To find the sum of N Natural Numbers we will use a simple loop with condition based on the range defined by the user.


Algorithm:-
- Take two inputs.
- Inputs are stored in two int type variable say lower_limit and upper_limit.
- Initialize i to lower_limit
- i=lower_limit
- Initialize sum to 0
- run a for loop till i is less than or equal to upper_limit, i<=upper_limit
- update sum +=i
- increment i by 1 in every iteration
- sum is printed being the sum of Natural Numbers between lower_limit and upper_limit.
C++ Code:-
//C++ Program
//Sum of Natural Numbers in a given range
#include<iostream>
using namespace std;
//main Program
int main()
{
int sum = 0 , upper_limit, lower_limit;
cout << “Enter the lower limit: “;
cin >> lower_limit;
cout << “Enter the upper limit: “;
cin >> upper_limit;
//calculating sum of numbers in the given range
for(int i = lower_limit; i <= upper_limit; i++){
sum += i;
}
//printing output
cout<<“The Sum of Natural Numbers from “ << lower_limit << ” to “ << upper_limit << ” is “ << sum;
return 0;
}
Output:-
Enter the lower limit: 19
Enter the upper limit: 65
The Sum of Natural Numbers from 19 to 65 is 1974
- Positive or Negative number: C | C++ | Java
- Even or Odd number: C | C++ | Java
- Sum of First N Natural numbers: C | C++ | Java
- Sum of N natural numbers: C | C++ | Java
- Sum of numbers in a given range: C | C++ | Java
- Greatest of two numbers: C | C++ | Java
- Greatest of the Three numbers: C | C++ | Java
- Leap year or not: C | C++ | Java
- Prime number: C | C++ | Java
- Prime number within a given range: C | C++ | Java
- Factorial of a number: C | C++ | Java
- Sum of digits of a number: C | C++ | Java
- Reverse of a number : C | C++ | Java
- Palindrome number: C | C++ | Java
- Armstrong number : C | C++ | Java
- Armstrong number in a given range : C | C++ | Java
- Fibonacci Series upto nth term : C | C++ | Java
- Factorial of a number : C | C++ | Java
- Power of a number : C | C++ | Java
- Factor of a number : C | C++ | Java
- Strong number : C | C++ | Java
- Perfect number : C | C++ | Java
- Automorphic number : C | C++ | Java
- Harshad number : C | C++ | Java
- Abundant number : C| C++ | Java
- Friendly pair : C | C++ | Java

Login/Signup to comment