I am trying to build a program that outputs Bish for any number that is divisible by 3, Bash for numbers divisible by 5 and BishBash for numbers divisible by both 3 and 5, and Bosh for each digit of the number adds up to an odd number.
The problem I'm facing is that I am struggling to figure out how to output BishBashBosh to do the above steps.
Here is an example of what it should do:
- 30 / 3 = 10 so 30 is divisible by 3: Bish
- 30 / 5 = 6 so 30 is divisible by 5: Bash
- 30 contains the digits 3 and 0. 3 + 0 = 3, which is odd: BOSH
In summary, the number 30 should output BishBashBosh in the console with the others as stated above.
Here is what I coded so far:
int Bish = 0;
int Bash = 0;
int BishBash = 0;
int BOSH = 0;
int numbers = 0;
for (int i = 1; i <= 15; i++)
{
int sumOfDigits = 0;
int temp = i;
while (temp > 0)
{
sumOfDigits += temp % 10;
temp != 10;
}
if (i % 3 == 0 && i % 5 == 0) // divisible by 3 and 5
{
Console.WriteLine("BishBash");
BOSH++; // increase counter for "BOSH" output
BishBash++;
}
else if (i % 3 == 0) // divisible by 3
{
Console.WriteLine("Bish");
Bish++;
}
else if (i % 5 == 0) // divisible by 5
{
Console.WriteLine("Bash");
Bash++;
}
else if (sumOfDigits % 2 == 1) // digits add up to an odd number
{
Console.WriteLine("BOSH");
BOSH++;
}
else // other number
{
Console.WriteLine(i);
}
}
Console.WriteLine($"{Bish}, {Bash}, {BishBash}, {BOSH}"); // print count for each output word
I experimented with the program in another project to try to fix the problem.
Here is a sample code of this implementation:
int n = 30;
int numbers = 0;
string output = "";
for (int i = 30; i <= 300; i++)
{
if (n % 3 == 0)
{
output += "Bish";
}
Console.WriteLine(output);
if (n % 5 == 0)
{
output += "Bash";
}
int sumOfDigits = 0;
while (n > 0)
{
sumOfDigits += n % 10;
n /= 10;
}
if (sumOfDigits % 2 == 1)
{
output += "BOSH";
}
else
{
Console.WriteLine(numbers);
}
}
Console.WriteLine(output);
The output in the console came up with the lots of lines of BishBashBosh
which was most definitely not the output I was expecting as it should output the numbers as well and only output it for numbers that are divisible by 3, 5 and the numbers added up for it to be an odd number.
The unexpected output is shown below:
BishBashBOSHBishBashBishBash which is repeated until it the loop is broken. this is shown in the Watch window in the debugger