15,748,973 members
See more:
I have to write a program that calculates the sum of all the integers in a given range and is also evenly divisible by a given integer. The user supplies the beginning, ending and the divisor integer.

I created two classes for this- the program class that accepts the inputs from the user and the summation class that does all the calculations.

I can't figure out how to write the code for the method in the summation class! any help would be appreciated!

Thanks! :)

Edit 1=

Thanks for the help guys! It's not homework. It's like a practice project that I'm doing for a class that I'll take next semester.

I know how to set up the loop for predefined values, but I can't figure out how to work the loop for user acceptable values.

Here's what I have so far:

The program class:
C#
```Summation

sum = new Summation();

Console.WriteLine("Please enter the lower limit of the integer range.");

Console.WriteLine("Please enter the upper limit of the integer range.");

Console.WriteLine("Please enter the divisor(the number that is to divide evenly into the values in this range).");

sum.Summation();

Console.WriteLine("The range of numbers that divide into your chose divisor is:{0}.", sum.Sum);```

The summation class:

C#
```class

Summation
{
public int lowerLimit, upperLimit, divisor, sum;
public int LowerLimit
{
get { return lowerLimit; }
set { lowerLimit = value; }
}

public int UpperLimit
{
get { return upperLimit; }
set { upperLimit = value; }
}

public int Divisor
{
get { return divisor; }
set { divisor = value; }
}
public int Sum
{
get { return sum; }
set { sum = value; }
}

private float restart;

public float Restart
{
get { return restart; }
set { restart = value; }
}

int Sum = 0;
for (int i = LowerLimit; i <= UpperLimit; i++)
{
Sum = Sum + i;
}```

EDIT 2 :

I tried putting this code in (attached below) but it doesn't work. :/ I get several errors. I just can't figure out this piece of code. Tried everything Griff suggested too. Anything else that can help me? I will forever be greatful for any help!
```  int Sum = 0;

for (int i = lowerLimit; i <= upperLimit; i++)
{
if(i % divisor == 0)
Sum = Sum + i;
}
return Sum;

<pre>```
Posted
Updated 29-Sep-18 4:50am
v4
Richard C Bishop 29-Nov-12 13:57pm
You will need to provide the code you have in order for someone to help you.
lewax00 29-Nov-12 14:19pm
So is it C# or Objective-C? Those are two entirely different languages...please fix your tags unless you really mean both.

## Solution 1

This smells heavily of homework, so no code!

It's pretty simple though:

1) Set up a total, equal to zero.
2) Set up a loop, starting at the user low value
3) If the loop value modulo the divisor is zero, add the loop value to the total
4) Increment the loop value. If it is less than or equal to the user high value, go back round to 3

## Solution 3

Well you're mostly there it looks like, but it looks like you're missing step 3 in Griff's answer. What you need for that is the Modulo Operator[^]. Using this you can get the remainder of the division of two numbers, and if that result is 0 then it can divide evenly (because, as I'm sure you know, if it divides evenly then there is no remainder).

## Solution 4

C#
```int Sum = 0;
for (int i = UserInput1; i <= UserInput2; i++)
{
if(i%divisor == 0)
Sum = Sum + i;
}
return Sum;```

--SJ

## Solution 2

C#
```int Sum = 0;
for (int i = UserInput1; i <= UserInput2; i++)
{
Sum = Sum + i;
}
//return the sum divided by the divisor
return Sum / divisor;
```

v2
lewax00 29-Nov-12 14:25pm
That's not what they were looking for...try reading their question again. Plus your solution adds an extra UserInput1 to the Sum (the first iteration will add that amount, but it was already set to that amount).

## Solution 5

This is a very old question, but none the less my two cents:

C#
`Enumerable.Range(lowerLimit, upperLimit).Where( n => n % divideableBy == 0).Sum();`