Problem when printing a factorial series [closed]

0

I want to print this factorial series: 2, 3, 8, 30 in C ++. I put this line to take the series and it does not work:

return factorial (número) + factorial (numero-1);

This is to get the values of the series based on the terms from 1 to 10. For example, if you use the number 4, you give us 30 because the 30 is the fourth element of the series: (2, 3 , 8, 30), and 4! + (4-1)!

This is my code:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

unsigned long factorial( unsigned long);

int main(){

  for( int i=0; i<=10;i++)
    cout<<i<<"!"<<factorial (i)<<endl;

  return 0;
}

unsigned long factorial (unsigned long numero) {
  if( número<=1)
    return 1;
  else 
    return factorial ( número) + factorial ( numero-1);
    // Se pone esta función, para que en base al término de la serie, se realice 
    // la operación, es decir si se usa la cuarta posición nos da 30, 
    // el 30 es el cuarto elemento de la serie : (2,3,8,30 ), 4! + (4-1)!
}
    
asked by Laura 06.04.2018 в 20:14
source

2 answers

0
// Example program
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;

unsigned long factorial(unsigned long);
int main(){
    for(int i=1; i<=10; i++) {
        cout << i << "!" << factorial (i) + factorial (i - 1) << endl;
    }
    return 0;
}


unsigned long factorial (unsigned long numero) {
    if ( numero <= 1) {
        return 1;
    } else { 
       return  numero * factorial ( numero-1) ;
    }
 // Se pone esta función, para que en base al término de la serie, se realice la operación, es decir si se usa la cuarta posición nos da 30, el 30 es el cuarto elemento de la serie : (2,3,8,30 ), 4! + (4-1)!
}

I guess the program you need this, to get the series (2,3,8,30). The series such that having n belonging to the numbers 1-10, we want to obtain n! + (n-1)!

The contribution of turkey was missing the part of the sum and the factorial of n - 1. Also, when setting the "unsigned long" function, it will give you an execution error if you start at i = 0, because the first parameter that you will pass is -1.

    
answered by 06.04.2018 / 21:18
source
0
// Example program
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;

unsigned long factorial( unsigned long);
int main(){
    for(int i=0;i<=10;i++) {
        cout<<i<<"!"<<factorial (i)<<endl;
    }
    return 0;
}


unsigned long factorial (unsigned long numero) {
    if ( numero <= 1) {
        return 1;
    } else { 
        return  numero * factorial ( numero-1) ;
    }
// Se pone esta función, para que en base al término de la serie, se realice la operación, es decir si se usa la cuarta posición nos da 30, el 30 es el cuarto elemento de la serie : (2,3,8,30 ), 4! + (4-1)!
}

In the second condition you were adding instead of multiplying, remember that this is the factorial. you were also returning twice the factorial return factorial ( número) + factorial ( numero-1); that would not be valid either because the factorial is n * n-1 * ..... Remember to also tabulate your code, this allows you and other people to read it easily.

    
answered by 06.04.2018 в 20:59