How to Solve Summation Problems in C++

Haris Iftikhar
1 min readJan 10, 2021

So for instance if we are asked to calculate the divisibility of the summation between two different numbers inclusively, we can adopt the following procedure:

First, we will include our boilerplate code:

#include <iostream>

using namespace std;

int main()

{

return 0;

}

After that we will declare four int for the following:

int test; //total number of test cases

int sum; //sum of the numbers in the range

int num1;//the first number

int num2;//the second number

now,

we will use a loop to loop around the specified number of times you want to run the code:

for(int i = 0; i<test; i++)

{

}

or

while(test — ) //minus minus

{
}

Now after all of that, here comes the main logic,

int main()

{

int test, num1, num2, sum;

cin>>test;

while(test — )

{

cin>>num1>>num2;

for(int j = num1; j≤num2; j++)

{

if(j%3==0)

{

sum += j; // or sum=sum+j;

}

}

cout<<” Summation of the numbers divisible by 3 between ”<<num1<<”and”<<num2<<”=”<<sum<<endl;

return 0;

}

--

--