Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Need help with a factorial program

I'm just starting and i need help with this problem:

Write a recursive function Factorial() that returns the factorial (a long) of its int argument. The factorial of a positive integer is the product of it and all the positive integers less than it. For example, the factorial of 5 is 5*4*3*2*1 or 120. The symbol for factorial is the exclamation point, therefore 5!=120. Computing this factorial can also be thought of as 5!=4! or more generally, N!=N*(N-1)! By definition, 0! is equal to 1.

Any help would be great. Thanks.

Comments

  • Geeth AsokanGeeth Asokan Member Posts: 127
    Hi,

    Something like this:

    [code]
    #include

    long factorial(long a)
    {
    if (a > 1)
    return (a * factorial(a-1));
    else
    return (1);
    }

    int main()
    {
    // Declaring the variables
    long l;
    std::cout << " Please enter a positive number: ";
    std::cin >> l;

    std::cout << l << "! = " << factorial(l) << std::endl;

    return 0;
    }
    [/code]

  • MeldrozMeldroz Member Posts: 38
    Hey buddy well i'll give you something to start on ok

    long Factorial( long& n)
    {
    // test if n is 0 , then you have to return 1 right?

    else
    return n times myself(n-1) // recursive call here
    }

    not exactly the most efficient way to do it but it's probably what your
    learning now.

    hope it helps,
    Mel


  • Justin BibJustin Bib USAMember Posts: 0

    _ [ http://forcoder.org ] free ebooks and video tutorials about || Java, Python, C, PL/SQL, Visual Basic, Swift, Perl, JavaScript, C#, C++, Ruby, R, Visual Basic .NET, Assembly, Go, Delphi, Objective-C, Scratch, MATLAB, PHP SAS, Lisp, Prolog, Hack, Bash, Lua, Awk, Apex, Rust, Kotlin, Logo, Scala, FoxPro, D, Alice, VBScript, ML, F#, Erlang, Crystal, Dart, ABAP, Ada, Transact-SQL, Scheme, Fortran, LabVIEW, Julia, Clojure, COBOL || _________

  • Ali UsulAli Usul Member Posts: 2

    include <stdio.h>

    include <conio.h>

    main ()
    {
    long i, n, faktor;
    printf ("Enter the number to be factoried: ");
    scanf ("%ld", &n);
    faktor=1;
    for (i=1; i<=n; i++)
    {
    faktor = i; /n! = 1x2x3x....xn */
    }
    printf ("%ld! = %ld\n", n, faktor);

    }

  • Ali UsulAli Usul Member Posts: 2

    doesn't work

Sign In or Register to comment.