-1

I'm trying to get all the factors of a number provided by a user and after a button click displaying all the factors on a MessageBox.

This is what my code code taken from Getting Factors of a Number answer by Mark Byers looks like, I thought I was pretty close, but I'm displaying the number of factors not the actual factor numbers.

For example if the user types in 10 and uses a button click, the MessageBox displays the number 4, but i'm wanting it to display the actual factors of 10 which should be 1,2,5,10.

*how can I display the factors?

public int Divisors(int fact)
    {
        int number = int.Parse(textBox2.Text);
        int factorCount = 0;
        int sqrt = (int)Math.Ceiling(Math.Sqrt(number));

        for (int i = 1; i < sqrt; i++)
        {
            if (number % i == 0)
            {
                factorCount += 2; //  We found a pair of factors.
            }
        }

        // Check if our number is an exact square.
        if (sqrt * sqrt == number)
        {
            factorCount++;
        }

        return factorCount;
    }

private void ShowallButton_Click(object sender, EventArgs e)
    {
        int input = int.Parse(textBox2.Text);
        double output = Divisors(input);

        MessageBox.Show(+output + "");
    }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    Instead of updating factor count, why don't you store the factor in an array and then display that array (by maybe converting to string). – peeyush singh Dec 12 '18 at 03:48
  • You also need to correct the for loop to `for (int i = 1; i <= number; i++)` – Chetan Dec 12 '18 at 03:54
  • well.... i'm still new at C# and I haven't really figured out how to use arrays.... – Christian Hernandez Dec 12 '18 at 03:56
  • You can start figuring that out by solving this issue... if not array you can use string to generate the output and return it from the method. – Chetan Dec 12 '18 at 03:57
  • I seriously doubt that it is "your code" : https://stackoverflow.com/questions/4549482/getting-factors-of-a-number. SO requires attribution when copy/pasting code - attribution edited in. – Alexei Levenkov Dec 12 '18 at 04:39

3 Answers3

0
        public List<int> Divisors(int fact)
    {
        List<int> factors = new List<int>();
        int number = int.Parse(textBox2.Text);
        int factorCount = 0;
        int sqrt = (int)Math.Ceiling(Math.Sqrt(number));

        for (int i = 1; i < sqrt; i++)
        {
            if (number % i == 0)
            {
                factors.Add(i);
                factorCount += 2; //  We found a pair of factors.
            }
        }

        // Check if our number is an exact square.
        if (sqrt * sqrt == number)
        {
            factorCount++;
        }

        // return factorCount;
        return factors;
    }
    private void ShowallButton_Click(object sender, EventArgs e)
    {
        int input = int.Parse(textBox2.Text);
        List<int> factors = Divisors(input);
        string message = $"The Divisors are {string.Join(",", factors)}";
        MessageBox.Show(message);
    }
George Fabish
  • 409
  • 2
  • 14
0

This is not very close at all.

Lets look at the factorisation first

% Operator (C# Reference)

The remainder operator % computes the remainder after dividing its first operand by its second operand.

public static void Divisors(int number )
{
   for (var x = 1; x <= number; x++)
      if (number % x == 0) // no remainder it must be a factor
         Console.WriteLine(x);
}

usage

Divisors(10);

Output

1
2
5
10

Now Lets return an Enumerable using yield

public static IEnumerable<int> Divisors(int number )
{
   for (var x = 1; x <= number; x++)
      if (number % x == 0) // no remainder it must be a factor
         yield return x;
}

Usage

var results = Divisors(10);

Console.WriteLine(string.Join(", ", results));

Output

1, 2, 5, 10

Additional resources

yield (C# Reference)

When you use the yield contextual keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator. Using yield to define an iterator removes the need for an explicit extra class (the class that holds the state for an enumeration, see IEnumerator for an example) when you implement the IEnumerable and IEnumerator pattern for a custom collection type.

String.Join Method

Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0
private void ShowallButton_Click(object sender, EventArgs e)
    {
        var input = int.Parse(textBox2.Text);
        var output = Divisors(input);

        var message = string.Join(Environment.NewLine, output);
        MessageBox.Show(message);
    }

    public List<int> Divisors(int number)
    {
        var factors = new List<int>();
        for (var i = 1; i <= number; i++)
        {
            if (number % i == 0)
            {
                factors.Add(i);
            }
        }
        return factors;
    }
Nimantha
  • 13
  • 4