In-range Adder

In-range Adder in C++

#include <iostream>

int inRangeAdder(int start, int end) {
    int sum = 0;
    for (int i = start; i <= end; ++i) {
        sum += i;
    }
    return sum;
}

int main() {
    int start = 1;
    int end = 5;
    int result = inRangeAdder(start, end);
    std::cout << "The sum of numbers from " << start << " to " << end << " is: " << result << std::endl;
    return 0;
}
  1. Include the necessary header file for input/output stream.
  2. Define a function inRangeAdder that takes two integers start and end as parameters and returns an integer.
  3. Initialize a variable sum to store the sum of numbers.
  4. Use a for loop to iterate from start to end, adding each number to the sum.
  5. Return the final sum.
  6. In the main function, define start and end variables.
  7. Call the inRangeAdder function with start and end as arguments and store the result in the result variable.
  8. Output the result using the standard output stream.

I hope this helps!