Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Program on factorial-Pascal programming?

Hey guys and gals,

I am having some problems to make this program on Factorial to run coz it is an 'infinite' formula like

fac(n):=n*(n-1)*(n-2)*...*2*1

I can't write a formula like this in pascal coz there's ... in it.

So can u give me a program code for a simple factorial calculation without the use of a function?

5 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Hey Arty,

    I'm fine, thank you for asking.

    :)

    There is no such thing as '...' in pascal, and the three dots (normally called ellipsis) are usually used to represent repetition in text.

    However, a function to calculate a factor is detailed below:

    function Factor(Cap: integer): integer;

    var n,v: integer;

    begin

    v:=Cap; n:=0;

    while v>0 do

    begin

    n:=n*v;

    Dec(v);

    end; {while}

    Factor:=n;

    end; {Factor}

    Hope this helps.

    ---edit---

    I realise there is a recursive solution. However, not only does recursion introduce unnecessary code complexity; it's also not normally the most efficient way of programming the solution. - In addition while loops are consistently faster and more flexible than for loops.

    na

    Source(s): *Remember to award Best Answer if it works for you.
  • 1 decade ago

    You don't write n*(n-1)...

    That's only to give you the algorigthim and approach how it should work. You have to translate that information logic into your pascal programming language. Some mathematical functions work as is where you can easily make them into a function but this is a recursive function which takes a special case. Recursion is a little difficult to understand because you have call the same function withing itself and also understand how the call stack works.

    I can't recall my pascall but here is a c++ version of it

    int factorial(int number) {

    int temp;

    // This is the base case, all recursive functions need a base case

    // otherwise you'll get an infinite loop

    // in Math terms from your formula fac(1) = 1*(1-1)*(1-2)... = 1

    if(number <= 1) return 1;

    /// this is the n(n-1) portion of your mathematical formula

    temp = number * factorial(number - 1);

    return temp;

    }

    This is one of the hardest concepts in math and harder in programming because the translation to a function. Since I gave you the answer study it until you see because if not other recursive functions, will give you the same headache. When the time comes that you'll need to create or recognize one you will not know how to solve the problem.

    Good luck!

  • Adam S
    Lv 4
    1 decade ago

    Be aware that while calculating factorials by recursion may be done as an example in textbooks, you would not want to implement it that way in practice in most programming languages because the factorial calculation is much faster to do in a simple iterative loop. The recursive solution adds the overhead of a function call for each multiplication step and also has the potential for stack overflow with large number input.

  • Tenax
    Lv 5
    1 decade ago

    Hi, there are at least two ways of writing a function for this.

    This is a recursive function, it will call itself:

    function fac(n: longint):longint;

    begin

    if n<2 then fac:=1

    else fac:=n * fac(n-1)

    end;

    The same result can be achieved in an iterative way (without recursion):

    function fac(n: longint):longint;

    var i,temp:longint;

    begin

    temp:=1;

    for i:=1 to n do temp:=temp*i;

    fac:=temp

    end;

    The difference of these two ways is in memory management, the recursive variant will take a lot more memory as it calls itself n-1 times.

    The ... in the above formula only means that you are supposed to find an algorithm that does the same as if you multiplied each of those factors by hand.

    I also used the LONGINT type for both the argument and the result, as the result will become very big very fast.

    Hope I could help

    Source(s): professional programmer
  • How do you think about the answers? You can sign in to vote the answer.
  • 1 decade ago

    There is two ways to do this the iterative way and the recursive way - what you need is a function as factorials over 8 are greater than the maxium Integer value it

    Function Factorial ( x: Integer) : Real;

    var

    i : integer; {''i'' here stands for iteration}

    fact : real; {Usually factorials are so large that we

    can't use integer here}

    begin

    fact:=1; {As we can't multiply by 0, our first number should be 1}

    for i:=1 to x do {Here is our loop}

    fact :=fact*i;

    Factorial = fact;{Value is returned }

    End;

    The recursive version of the function

    Function Factorial (N: Integer): Real

    (* RECURSIVE COMPUTATION OF N FACTORIAL *)

    BEGIN

    (* TEST FOR STOPPING STATE *)

    If n <= 0 then

    Factorial := 1

    Else

    Factorial := n * Factorial(n - 1)

    End; (* FACTORIAL *)

    The recursive version is a little more elegant but is the more memory hungry version in old computers the bigger the number the slower it went.

Still have questions? Get your answers by asking now.