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:
Summation
sum = new Summation();
Console.WriteLine("Please enter the lower limit of the integer range.");
sum.LowerLimit = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Please enter the upper limit of the integer range.");
sum.UpperLimit = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Please enter the divisor(the number that is to divide evenly into the values in this range).");
sum.Divisor = Convert.ToInt16(Console.ReadLine());
sum.Summation();
Console.WriteLine("The range of numbers that divide into your chose divisor is:{0}.", sum.Sum);
The summation class:
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>