0

I need to write a factorial using an array but the problem is only Show 120 five times and what i want is 1, 2, 6, 24, 120 showing in the textBox

int factProg = 1;
public void factArray(int[] arr)
{   
    for (int i = 0; i < arr.Length; i++)
    {
        factProg = factProg * arr[i];
    }
}



int[] arr = {1,2,3,4,5};

for (int i = 0; i < arr.Length; i++)
{
    factArray(arr);
    textBox1.Text += Convert.ToString(factProg);
    textBox1.Text += Environment.NewLine;
}
15
  • What are you trying to do? Does this even compile? UPD: forgot about methods inside methods, but that is some amazing code style. Commented Nov 17, 2017 at 8:57
  • You are having two loops, ofcourse it will print 120 five times Commented Nov 17, 2017 at 8:58
  • i want factorial from 1 to 5 showing in the textbox @someone using this method Commented Nov 17, 2017 at 8:58
  • so how I can fix it @OfirWinegarten ? Commented Nov 17, 2017 at 8:59
  • 1
    Rather than calculating a factorial for each n from 1 to 5, you calculate the product of array [1,2,3,4,5] 5 times. You could use int fact(int i)=>i*fact(i-1); and then in the for loop fact(arr[i]) instead of factArray(arr) Commented Nov 17, 2017 at 8:59

2 Answers 2

2

Simply put - your factArray method always calculates the factorial for the length of the whole array which is a fixed size. You need to pass an extra parameter - being the position in the array you want to stop calculating

Try this if you want to calculate using your array :

int factProg;
public void factArray(int[] arr, int len)
{   
  factProg = arr[0];
  for (int i = 1; i <= len; i++)
  {
    factProg = factProg * arr[i];
  }
}

int[] arr = {1,2,3,4,5};

for (int i = 0; i < arr.Length; i++)
{
  factArray(arr, i);
  textBox1.Text += Convert.ToString(factProg);
  textBox1.Text += Environment.NewLine;
}

You would need to add a check in the method that you are not exceeding the array length.

Sign up to request clarification or add additional context in comments.

2 Comments

Code cleanup suggestion: make factProg local to the method and return its value.
@HansKesting: I deliberately kept the code as close as possible to the original - but that's not the only thing I would do to clean up the code.
0

Please find the code below

public static void FactorialJohn(int[] arr)
            {          

                List<int> Resarr = new List<int>();
                arr.ToList().ForEach(x => Resarr.Add(Enumerable.Range(1, x).Aggregate((a, b) => a * b)));

                foreach (var item in Resarr)
                {
                    Console.WriteLine(item);
                }
            }

The above code gets you factorial of every element in the array.

1 Comment

@John The above code will get you the same result using Linq

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.