patterncsharpMinor
Check if divisible
Viewed 0 times
divisiblecheckstackoverflow
Problem
Description
My program checks the sum of 2 numbers to determine if it is divisible by a certain number (5 in this case). Divisible numbers are deemed usable (for another program I am running). Next it checks the individual integers to see if those are divisible/usable as well.
Problem
I want to use this same code for divisors 2 - 9 (each will have their own class). The problem is that this code seems a little repetitive and lengthy to do for each divisor.
I want to know if there is a more simplistic way of generating the same output. Or is my answer found in creating individual classes for each action (that way each change will be reflected across the divisors classes)?
Code
```
static void Main(string[] args)
{
Challenge(7, 7);
}
static void Challenge(int num1, int num2)
{
//finds the sum of the two variables
int sum = num1 + num2;
Console.WriteLine("The sum of {0} and {1} is...\n{2}", num1, num2, sum);
#region SumCheck
bool isDivisible;
//checks if divisible by 5 and sets a value for 'isDivisible'
if (sum % 5 == 0)
{
Console.WriteLine("\nThe sum is divisible by 5!");
isDivisible = true;
}
else
{
Console.WriteLine("\nThe sum is not divisible by 5!");
isDivisible = false;
}
//depending on value of 'isDivisible', returns certain functions
if (isDivisible)
{
Console.WriteLine("This value is usable.");
Console.WriteLine("\n\nThe remaining usable values are: ");
for (int newVal = 0; newVal <= 55; newVal++)
{
if ((newVal % 5 == 0) && (newVal != sum))
{
Console.WriteLine(newVal);
}
}
}
else
{
Console.WriteLine("This value is not usable.");
Console.WriteLine("\n\nThese values are considered usable: ");
for (int newVal = 0; newVal <= 55; newVal++)
{
if (newVal % 5 == 0)
{
Console.WriteLine(newVal)
My program checks the sum of 2 numbers to determine if it is divisible by a certain number (5 in this case). Divisible numbers are deemed usable (for another program I am running). Next it checks the individual integers to see if those are divisible/usable as well.
Problem
I want to use this same code for divisors 2 - 9 (each will have their own class). The problem is that this code seems a little repetitive and lengthy to do for each divisor.
I want to know if there is a more simplistic way of generating the same output. Or is my answer found in creating individual classes for each action (that way each change will be reflected across the divisors classes)?
Code
```
static void Main(string[] args)
{
Challenge(7, 7);
}
static void Challenge(int num1, int num2)
{
//finds the sum of the two variables
int sum = num1 + num2;
Console.WriteLine("The sum of {0} and {1} is...\n{2}", num1, num2, sum);
#region SumCheck
bool isDivisible;
//checks if divisible by 5 and sets a value for 'isDivisible'
if (sum % 5 == 0)
{
Console.WriteLine("\nThe sum is divisible by 5!");
isDivisible = true;
}
else
{
Console.WriteLine("\nThe sum is not divisible by 5!");
isDivisible = false;
}
//depending on value of 'isDivisible', returns certain functions
if (isDivisible)
{
Console.WriteLine("This value is usable.");
Console.WriteLine("\n\nThe remaining usable values are: ");
for (int newVal = 0; newVal <= 55; newVal++)
{
if ((newVal % 5 == 0) && (newVal != sum))
{
Console.WriteLine(newVal);
}
}
}
else
{
Console.WriteLine("This value is not usable.");
Console.WriteLine("\n\nThese values are considered usable: ");
for (int newVal = 0; newVal <= 55; newVal++)
{
if (newVal % 5 == 0)
{
Console.WriteLine(newVal)
Solution
Why would you make each one it's own class? All you need is a property:
Then, wherever you have "5" in you code, use
If you don't have access to C#6.0 (which the above requires) you can use:
Then, define your challenge method:
If you also want to auto-generate that
You can do:
Which will give you the same format of equality. (E.g.:
You should also consider adding an exception in the constructor we defined above if
public int Divisor { get; }Then, wherever you have "5" in you code, use
Divisor. This means you could make a constructor:public ClassName(int divisor)
{
Divisor = divisor;
}If you don't have access to C#6.0 (which the above requires) you can use:
public int _divisor;
public int Divisor { get { return _divisor; } }
public ClassName(int divisor)
{
_divisor = divisor;
}Then, define your challenge method:
public void Challenge(int num1, int num2)
{
// code here
}If you also want to auto-generate that
55 in:for (int newVal = 0; newVal <= 55; newVal++)You can do:
for (int newVal = 0; newVal <= _divisor * 11; newVal++)Which will give you the same format of equality. (E.g.:
<= 22 for 2, etc.)You should also consider adding an exception in the constructor we defined above if
divisor is outside the range you would like.Code Snippets
public int Divisor { get; }public ClassName(int divisor)
{
Divisor = divisor;
}public int _divisor;
public int Divisor { get { return _divisor; } }
public ClassName(int divisor)
{
_divisor = divisor;
}public void Challenge(int num1, int num2)
{
// code here
}for (int newVal = 0; newVal <= 55; newVal++)Context
StackExchange Code Review Q#113202, answer score: 4
Revisions (0)
No revisions yet.