Write an Pascal Program for a factorial function usingRecursive Subprograms - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Saturday, September 23, 2017

Write an Pascal Program for a factorial function usingRecursive Subprograms


Factorial Of An Number Using  Recursive Subprograms


Program:
program exRecursion;
var
num, f: integer;
function fact(x: integer): integer; (* calculates factorial of x - x! *)

begin
if x=0 then
fact := 1
else
fact := x * fact(x-1); (* recursive call *)
end; { end of function fact}

begin
writeln(' Enter a number: ');
readln(num);
f := fact(num);

writeln(' Factorial ', num, ' is: ' , f);
end.
Output:
Enter a number:
5

Factorial 5 is: 120

No comments:

Post a Comment