Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm struggling with this question about arithmetic

The function must return the sum of all integers between from and to including these numbers.
For example, arithmeticSum(2,4) should be 9 because 2+3+4 = 9.

This is the code right now, i can’t change anything in the main.

#include <iostream>
using namespace std;
int aritmetiskSumma(int from, int to){
    int i=0;
    int sum=0;
    for(i=1;i<to;i++){
        sum+=i;
    }
  return sum; // TODO
}
// Ändra inget nedanför denna rad
void provaAritmetiskSumma(){
    int from, to;
    cin >> from >> to;
    int summa = aritmetiskSumma(from, to);
    cout << summa << endl;
}
int main(){
    provaAritmetiskSumma();
    return 0;
}


MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In order to calculate the sum, adjust the for loop like this:

for(; from <= to; from++)
{
   sum += from;
}

This code starts with the from value and calculates the sum of all the integers up to and including to.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading