Search code examples
c++increment

I need to know how to add using an increment value up to a certain number in c++


I am having a hard time trying to add the starting value with the increment value up until it reaches the ending value or it cant add again because it would exceed the max value(the end value).

Ok I am just going to get straight to it, here is my assignment.

In this assignment, you will complete a C++ program that sums up the integers in a range of values and prints the result. This will be done two different ways: using a while loop and using a for loop.

For this assignment, you have more freedom in choosing the local variables that you need to declare and in figuring out what source code to write. However, your program needs to follow the coding standards of this course and fulfill the software requirements described in the next section.

Below is an example execution of the program. In this case, the program added up the numbers 8, 25, 42, 59, 76, 93, and 110. Your program shall follow the same format shown below for prompting the user and printing the results.

Enter a starting integer value: 8

Enter an ending integer value: 121

Enter a positive increment: 17

Sum (using a while loop): 413

Sum (using a for loop): 413

Here is what I have for code so far

#include <iostream>

using namespace std;

int main(){
  //while loop sum
  int sumw = 0;
  //for loop sum
  int sumf = 0;
  //starting integer
  int nums;
  //ending integer
  int nume;
  //increment integer
  int numi;

  cout <<"Please enter a starting value: " << endl;
  cin >> nums;
  cout <<"Please enter an ending value: " << endl;
  cin >> nume;
  cout <<"Please enter a positive increment value: " << endl;
  cin >> numi;

  if (numi <= 0 || nums > nume) cout << "Error ";
  if (numi <= 0 || nums > nume) return 0;

  for (int i = 1; i <= numi; i++){      
    sumf =+ numi;
  }
  cout << "Sum(using for loop): " << sumf;

  return 0;
}

If someone could help me with this that would be great!!! Thank you!!


Solution

  • Assumed the starting number as greater than or equal to 1(>=1). Using while loop:

    #include <iostream>
    using namespace std;
    int main()
    {
      int totalSum = 0, startingNumber, endingNumber, positiveIncrement;
    
      cout <<"Enter the starting number: " << endl;
      cin >> startingNumber;
      cout <<"Enter the ending number: " << endl;
      cin >> endingNumber;
      cout <<"Enter the positive increment: " << endl;
      cin >> positiveIncrement;
    
      if ((startingNumber <= 0) || (startingNumber > endingNumber))
      {
          cout<<"Error in input provided"<< endl;
          return 0;
      }
      totalSum = startingNumber;
      while ((startingNumber + positiveIncrement) <= endingNumber)
      {
          startingNumber += positiveIncrement;
          totalSum += startingNumber;
      }
      cout << "Total Sum = " << totalSum;
      return 0;
    }
    

    Using for loop:

    #include <iostream>
    using namespace std;
    int main()
    {
      int totalSum = 0, startingNumber, endingNumber, positiveIncrement;
    
      cout <<"Enter the starting number: " << endl;
      cin >> startingNumber;
      cout <<"Enter the ending number: " << endl;
      cin >> endingNumber;
      cout <<"Enter the positive increment: " << endl;
      cin >> positiveIncrement;
    
      if ((startingNumber <= 0) || (startingNumber > endingNumber))
      {
          cout<<"Error in input provided"<< endl;
          return 0;
      }
      for ((totalSum = startingNumber);((startingNumber + positiveIncrement) <= endingNumber);(startingNumber += positiveIncrement))
      {
          totalSum += (startingNumber+positiveIncrement);
      }
      cout << "Total Sum = " << totalSum;
      return 0;
    }