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);
}
}
}
#endregion
#region NumCheck
bool isNumDivisible;
//checks if divisible by 5 and sets a value for 'isNumDivisible'
if ((num1 % 5 == 0) && (num2 % 5 == 0))
{
Console.WriteLine("\n\n\n{0} and {1} are both divisible by 5!", num1, num2);
isNumDivisible = true;
}
else if ((num1 % 5 == 0) && (num2 % 5 != 0))
{
Console.WriteLine("\n\n\nOnly {0} is divisible by 5!", num1);
isNumDivisible = true;
}
else if ((num1 % 5 != 0) && (num2 % 5 == 0))
{
Console.WriteLine("\n\n\nOnly {0} is divisible by 5!", num2);
isNumDivisible = true;
}
else
{
Console.WriteLine("\n\n\n{0} and {1} are both not divisible by 5!", num1, num2);
isNumDivisible = false;
}
//depending on value of 'isNumDivisible', returns certain functions
if (isNumDivisible)
{
//if both are true
if ((num1 % 5 == 0) && (num2 % 5 == 0))
{
Console.WriteLine("Both values are usable.");
Console.WriteLine("\n\nThe remaining usable values are: ");
for (int newVal = 0; newVal <= 55; newVal++)
{
if ((newVal % 5 == 0) && (newVal != num1) && (newVal != num2))
{
Console.WriteLine(newVal);
}
}
}
//if 'num1' is true and 'num2' is false
else if ((num1 % 5 == 0) && (num2 % 5 != 0))
{
Console.WriteLine("Only {0} is usable.", num1);
Console.WriteLine("\n\nThe remaining usable values are: ");
for (int newVal = 0; newVal <= 55; newVal++)
{
if ((newVal % 5 == 0) && (newVal != num1))
{
Console.WriteLine(newVal);
}
}
}
//if 'num2' is true and 'num1' is false
else
{
Console.WriteLine("Only {0} is usable.", num2);
Console.WriteLine("\n\nThe remaining usable values are: ");
for (int newVal = 0; newVal <= 55; newVal++)
{
if ((newVal % 5 == 0) && (newVal != num2))
{
Console.WriteLine(newVal);
}
}
}
}
//if both are false
else
{
Console.WriteLine("Both values are not usable.", num2);
Console.WriteLine("\n\nThese values are considered usable: ");
for (int newVal = 0; newVal <= 55; newVal++)
{
if (newVal % 5 == 0)
{
Console.WriteLine(newVal);
}
}
}
#endregion
Console.ReadLine();
}