0

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

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Dan
  • 49
  • 5
  • 4
    Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Aug 04 '23 at 13:48
  • 1
    So, you're attempting a variant of fizz buzz – phuzi Aug 04 '23 at 13:48
  • @phuzi exactly so – Dan Aug 04 '23 at 13:53

2 Answers2

1

Please, note that some values (liek 30) can be both Bish, Bash and Bash, that's why you should test for all three possibilities, i.e. 3 separted if, not if else. Let's extract method (Classify) for this:

private static string Classify(int value) {
  int sumOfDigits = 0;

  for (int number = value; number > 0; number /= 10)  
    sumOfDigits += number % 10;

  String result = "";
 
  // Note, that each condition has its own "if"
  if (value % 3 == 0)  
    result += "Bish";

  if (value % 5 == 0)  
    result += "Bash";

  if (sumOfDigits % 2 != 0) 
    result += "Bosh";

  return result;
}

Then you can put

// Put required limits here instead of mine 1 and 30
for (int i = 1; i <= 30; ++i)
    Console.WriteLine($"{i,2} : {Classify(i)}");  

Output

 1 : Bosh
 2 : 
 3 : BishBosh
 4 : 
 5 : BashBosh
 6 : Bish
 7 : Bosh
 8 : 
 9 : BishBosh
10 : BashBosh
11 : 
12 : BishBosh
13 : 
14 : Bosh
15 : BishBash
16 : Bosh

...

27 : BishBosh
28 : 
29 : Bosh
30 : BishBashBosh

Fiddle

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

The issue here is that your output is outside the for loop and gets added new and new text at each loop iteration. The console output is also outside the loop, and should be inside instead (since you want to print the result for each i). A fix might look like this:

class Program {
    static void Main() {
        for (int i = 30; i <= 300; i++) {
            string output = "";
            if (i % 3 == 0) {
                output += "Bish";
            }
            if (i % 5 == 0) {
                output += "Bash";
            }
    
            if (SumOfDigits(i) % 2 == 1) {
                output += "Bosh";
            }
    
            Console.WriteLine($"{i}: {output}");
        }
    }
    
    static int SumOfDigits(int num) {
        int sumOfDigits = 0;
        while (num > 0) {
            sumOfDigits += num % 10;
            num /= 10;
        }
    
        return sumOfDigits;
    }
}

Also note that you only have a fixed number n passed to your digits' sum finding—it should be i instead. Plus, it's better to put the digits' sum finding logic into a separate function.

The result then will be:

30: BishBashBosh
31:
32: Bosh
33: Bish
34: Bosh
35: Bash
36: BishBosh
37:
38: Bosh
39: Bish
40: Bash
41: Bosh
42: Bish
43: Bosh
44:
45: BishBashBosh
46:
47: Bosh
48: Bish
49: Bosh
50: BashBosh
51: Bish
52: Bosh
53:
54: BishBosh
55: Bash
...
ArtFul
  • 21
  • 6