Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My Code Always outputs a 0 I not know what is wrong

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);
  }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading