Breaking

Sunday 16 July 2017

Find the sum of multiples of X between A and B


Find the sum of multiples of X between A and B


Given the values for X, A and B, the program must print the sum of multiples of X from A to B (inclusive of A and B).

Input Format:
The first line contains the value of X.
The second line contains the value of A.
The third line contains the value of B.

Output Format:
The first line contains the sum of multiples of X from A to B (inclusive of A and B).

Boundary Conditions:
1 <= X <= 100
1 <= A <= 100000
A <= B <= 1000000

Example Input/Output 1:
Input:
5
10
25

Output:
70

Explanation:
The multiples of 5 from 10 to 25 are, 10,15,20,25 and their sum is 70

Code:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
    int n,a,b,i,sum=0;
    cin>>n>>a>>b;
    for(i=a;i<=b;i++)
        {
            if(i%n==0)
                sum+=i;
        }
    cout<<sum;


}

No comments:

Post a Comment

Like