Factorial N numbers in a for loop

I am working on a problem from CodeChef where I need to calculate a factorial of n numbers.

The user enters a number that determines how many ints does factorial calculation, and then enters numbers to calculate.

My problem is related to the multiplication itself. For example, if I have int == 5, then the result will be 20 (it will calculate n only by the last factorial, and not by all)

This is where the problem is:

for(int x = 0; x < _numbersToProcess.Length; x++) {// Loop throuigh Array by index for (int y = 1; y < _numbersToProcess[x]; y++) {// Y is equal to less than index x _result[x] = _numbersToProcess[x] * y;// Multiply x by y then add to array } } 

The outer loop determines how many calculations to perform.

The inner loop calculates factorials by repeating each _numberToProcess index and multiplying it by each number less than the number to be calculated.

The problem is that factorial calculation overwrites itself,

eg:

factorial of 5 result: 20 , but it should be 120 (it is overwritten until it reaches the last factor)

So, I tried the following:

 _result[x] = _numbersToProcess[x] *= y; 

This obviously matches _numbersToProcess[x] = _numbersToProcess[x] * y;

But this gives a completely different result:

If we enter 5 again, this will exit -1899959296.

I know that I can easily copy and paste from other materials, but I want to know why my method does not lead to the correct conclusion.

Here is the method as a whole:

 int _numbers = int.Parse(Console.ReadLine());// Get number of ints to calculate int[] _numbersToProcess = new int[_numbers];// Array of inputs int[] _result = new int[_numbers]; int i = 0; while(i < _numbersToProcess.Length) { _numbersToProcess[i] = int.Parse(Console.ReadLine()); i++; } for(int x = 0; x < _numbersToProcess.Length; x++) {// Loop throuigh Array by index for (int y = 1; y < _numbersToProcess[x]; y++) {// Y is equal to less than index x _result[x] = _numbersToProcess[x] *= y;// Multiply x by y then add to array } } for (int n = 0; n < _result.Length; n++) {// Y is equal to less than index x Console.WriteLine(_result[n]);// Write to console } Console.ReadLine(); 
+8
c # for-loop factorial console-application
source share
5 answers
 int _numbers = int.Parse(Console.ReadLine());// Get number of ints to calculate int[] _numbersToProcess = new int[_numbers];// Array of inputs int[] _result = new int[_numbers]; int i = 0; while(i < _numbersToProcess.Length) { _numbersToProcess[i] = int.Parse(Console.ReadLine()); i++; } for (int x = 0; x < _numbersToProcess.Length; x++) {// Loop throuigh Array by index int fact = 1; for (int y = 1; y <= _numbersToProcess[x]; y++) {// Y is equal to less than index x fact = fact*y; } _result[x] = fact; } for (int n = 0; n < _result.Length; n++) {// Y is equal to less than index x Console.WriteLine(_result[n]);// Write to console } Console.ReadLine(); 

The problem is with your inner loop. here you are always the main array of results.

ie for y = 5; the inner for loop executes 5 times.

 iteration -1 : y=1, _numbersToProcess[5]=5 _result[x]=5 iteration -2 : y=2, _numbersToProcess[5]=10 _result[x]=10 iteration -3 : y=3, _numbersToProcess[5]=30 _result[x]=30 . . . . . 

thus, it goes through 12 iterations, since your _numbertoprocess [5] changes and stops when it reaches less than 0 ie -1899959296.

 iteration 12: _numbertoprocess[5] = -1899959296. 

ie you change numbertoprocess every time in your inner loop.

you can check it by adding

 Console.WriteLine(y); Console.WriteLine(_numbersToProcess[x]); Console.WriteLine(_result[x]); 

in your inner loop.

+3
source share
 for (int y = 1; y < _numbersToProcess[x]; y++) {// Y is equal to less than index x _result[x] = _numbersToProcess[x] *= y;// Multiply x by y then add to array } 

In loop state y < _numberToProcess[x]; . It compares the value of y and _numberToProcess[x] array

I think you should edit the loop condition to y < x

Be happy.

+1
source share

Here I use the recursive function factorial

  /* Factorial function*/ int factorial (int n) { return (n*factorial(n-1)) } int _numbers = int.Parse(Console.ReadLine());// Get number of ints to calculate int[] _numbersToProcess = new int[_numbers];// Array of inputs int[] _result = new int[_numbers]; int i = 0; while(i < _numbersToProcess.Length) { _numbersToProcess[i] = int.Parse(Console.ReadLine()); i++; } for(int x = 0; x < _numbersToProcess.Length; x++) {// Loop throuigh Array by index _result[x] = factorial(_result[x])// Multiply x by y then add to array } } for (int n = 0; n < _result.Length; n++) {// Y is equal to less than index x Console.WriteLine(_result[n]);// Write to console } Console.ReadLine(); 
0
source share
 #include <stdio.h> int main() { int c, n, fact = 1; printf("Enter a number to calculate it factorial\n"); scanf("%d", &n); for (c = 1; c <= n; c++) fact = fact * c; printf("Factorial of %d = %d\n", n, fact); return 0; } 
-one
source share

Check it out, maybe this will help ...

 #include <stdio.h> #include <stdlib.h> long f(int n) { if (n==0) return 1; else return n * f(n-1); } int main(int argc, char *argv[]) { long *factorials; int *inputs; int n; printf("Enter number n = "); scanf("%d", &n); factorials = (long *) malloc(n*sizeof(long)); inputs = (int *) malloc(n*sizeof(int)); for (int i = 0; i < n; i++) { long k; printf("Enter %d number = ", i + 1); scanf("%ld", &k); inputs[i] = k; factorials[i] = f(k); } for (int i = 0; i < n; i++) { printf("Factorial for %d = %ld\n", inputs[i], factorials[i]); } return 0; } 
-2
source share

All Articles