Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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.");
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:

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
Comments
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.

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
5) Total is the answer.
 
Share this answer
 
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).
 
Share this answer
 
C#
int Sum = 0;        
for (int i = UserInput1; i <= UserInput2; i++)
{
if(i%divisor == 0)
    Sum = Sum + i;
}
return Sum;


--SJ
 
Share this answer
 
C#
int Sum = 0;
for (int i = UserInput1; i <= UserInput2; i++)
{
    Sum = Sum + i;
}
//return the sum divided by the divisor
return Sum / divisor;
 
Share this answer
 
v2
Comments
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).
This is a very old question, but none the less my two cents:

C#
Enumerable.Range(lowerLimit, upperLimit).Where( n => n % divideableBy == 0).Sum();
 
Share this answer
 
Comments
Patrice T 29-Sep-18 17:12pm    
Try to answer actual question, the OP didn't visit us for 5 years.
JVRO 30-Sep-18 5:07am    
Try to read the answer more carefully and
don't patronize people just because you're in your comfort zone.
What I do and don't is my decision not yours.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900