Home > Software design >  My Code Always outputs a 0 I not know what is wrong
My Code Always outputs a 0 I not know what is wrong

Time:06-10

I am trying to find the number of 3 digit numbers that has 2 numbers that are the factors of the third. So for example, 248 would be valid since 2 * 4 = 8 and 933 would also be valid since 3 * 3 = 9.

using System;

class Program
{
  public static void Main (string[] args) 
  {
    int Total = 0;
    for (int i = 100; i < 1000; i  )
    {
      string I = Convert.ToString(i);
      Console.WriteLine(I[2] " " I[1] " " I[0] " " I);
      if (I[2] == I[1] * I[0] || I[1] == I[0] * I[2] || I[0] == I[2] * I[1])
      {
        Console.WriteLine("true");
        Total  ;
      }
    }
    Console.WriteLine("the total is");
    Console.WriteLine(Total);
  }
}

CodePudding user response:

When you access a string by index, you get a char and not an int. To get an int out of a char, you have to parse it back:

var a = int.Parse(I[0].ToString());
var b = int.Parse(I[1].ToString());
var c = int.Parse(I[2].ToString());

Then you can do your multiplication like a == b * c and so on

  •  Tags:  
  • c#
  • Related