Computing.Net > Forums > Programming > Factorial in Pascal

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Factorial in Pascal

Reply to Message Icon

Name: goosh
Date: February 27, 2003 at 05:42:23 Pacific
OS: XP Pro
CPU/Ram: 700/512
Comment:

Hi I have a problem. It must be easy but I just can't get it. I have to write a simple program that will take a number and give the factorial but only using the 'for' and 'while' statements. Does anyone have any ideas???
Thanks :)



Sponsored Link
Ads by Google

Response Number 1
Name: elric
Date: February 27, 2003 at 06:12:51 Pacific
Reply:

G'day,

This might not be exactly what you want, but it does use for/next but no while.

program Fact2;
var
number:longint;
function fact(n:longint) : longint;
var
Index,Hold : longint;
begin
Hold:=1;
for Index := 1 to n do
Hold := Hold*Index;
fact := Hold;
end;
begin
for number := 1 to 10 do
writeln(number:2,'! =',fact(number));
end.
This is Turbo Pascal and uses the function fact to calculate the first 10 factorials.
There is another method using recursion that only uses the IF statement to calulate them.
Good luck
Elric



0

Response Number 2
Name: goosh
Date: February 27, 2003 at 13:03:02 Pacific
Reply:

Hey Elric thanks for the reply but this is my problem. I am just a beginner and I haven't got the the stage of logint yet. I am supposed to make a factorial program just using an integer variable. Seems a bit strange to me but that's what I have to do. EEK!!


0

Response Number 3
Name: borelli34
Date: February 27, 2003 at 14:00:56 Pacific
Reply:

goosh, You don't need to worry about it. Just use standard integer. The only difference is in the range represented and the number of bytes taken in memory.

borelli34


0

Response Number 4
Name: elric
Date: February 27, 2003 at 22:01:50 Pacific
Reply:

G'day,

Yes, longint is still an integer; it just has a much larger range.
Factorial calculations have a habit of getting very large, very quickly. You will soon run into overflow errors.
EG an unsigned integer would be 16bit ie a range of 0 to 64k.
I recently answered a query about calulating the amount of permutions of 12 items from a selection of 37. I can't remember the exact answer but it was approx in the order of 10^17 (that's ten to the power of 17!!)
In turbo pascal you have an integer (-32,768 to +32,767), word (0 to +65,535) and longint (-2,147,483,648 to +2,147,483,647) and also a range of real numbers, but I won't go into those.
So, as borelli34 has noted, you will be ok with unsigned int (or word), but the longint is there to avoid an overflow.
regards,
Elric


0

Response Number 5
Name: goosh
Date: February 28, 2003 at 05:33:01 Pacific
Reply:

Hi guys thanks for helping me out. I'm still a bit confused. I looked up the function in my book (which is quite small) and I understand that but what does the != syntax mean? Also I have to get input off the user and I don't know where I would put the writeln to ask for it and also which variable I would make the input? Sorry if this seems so basic but I really am at the beginning. Thanks again for your help.


0

Related Posts

See More



Response Number 6
Name: elric
Date: February 28, 2003 at 06:43:06 Pacific
Reply:

G'day,

Yes, that might look a bit confusing, but it is just a bit of formating:
writeln(number.2,'!=',fact(number));
Say we want the factorial of 10, then that line above will read-
10[number formated to 2 digits]!=3628800 [the value of 10 factorial(!)]
Anything between '' is just text, so without the braces:
10!=3628800
As for inputing data, number is the variablr you want, so something simple like:
writeln('Which factorial do you wish to calculate??');
readln(number);
Put this at the top and you can then pass number to the fact function.
You might want to add error checking at some point.
regards
Elric


0

Response Number 7
Name: elric
Date: February 28, 2003 at 07:03:00 Pacific
Reply:

G'day,

Just had another look at the code; the programme I posted goes through all the factorials from 1 to 10. For what you want, it will be a little different:

Program Fact2
(**************************)
(*Declare global vars here*)
var
number : longint; (* or integer*)
(***Define function Here*******)
function fact(n:longint) : longint;
var
Index,Hold:longint;
(**start function here***)
begin
Hold:=1;
for Index := 1 to n do
Hold:=Hold*Index;
fact:=Hold;
end;
(*****Main programme starts here*****)
begin
writeln('What factorial do you want to calculate??');
readln(number);
writeln(number.2,'!=',fact(number));
end.

I haven't checked that for errors (no pascal compiler loaded at the moment), but you get the idea.
Good luck,
Elric



0

Response Number 8
Name: goosh
Date: February 28, 2003 at 10:01:41 Pacific
Reply:

Hi Elric thanks very much for your help that code works great. Lol won't I look good now! Although to look good I need to know exactly what I'm doing. What is this code doing exactly? function fact(n:longint) : longint;

I am assuming you are making the 'function' "fact" a longinteger and that 'n' is what that variable is known as??? Is that why the 'n' is in brackets with the longint???

writeln(number.2,'!=',fact(number));

I don't know why there is a '2' after number and before the line '!=' ? What does this do?

Also I noticed I can only input a number up to and including 33. All above that return a 0.

I understand you are busy and thanks again for helping me but if you have a spare few minutes to roughly explain I would greatly appreciate it and give you shares in my first multi-million $ programming company lol. :)


0

Response Number 9
Name: elric
Date: March 2, 2003 at 10:24:06 Pacific
Reply:

G'day,
Well, I could certainly do with the money, so get going!!
The only thing that you really need to concern yourself with is the pascal syntax of function fact and the algorithm for factorials.
Starting with the algorithm:
############################
n!=n*(n-1)*(n-2)*....(3)*(2)*(1)
Eg: 6!=6*5*4*3*2*1
It can also be stated the other way Viz: 1*2*3...(n-2)*(n-1)*(n)
the program is:
********************
hold:=1
for index:= 1 to n
hold:=hold*index
fact:=hold
********************
hold is a temporary variable that is set to 1 initially in case it's initialised by Pascal as zero.
If you follow the logic, you will see that the value of hold is cumulative; which is what you want
ie 1*2 becomes [1*2]*3, which becomes [1*2*3]*4- values in square brackets being the successive values of hold as you go through the loop.
At the end of the loop (index=n)the value of index is assigned to fact and is then returned to the main program.
Now the function syntax
########################
function fact(n:longint) :longint
Pascal functions return only one value, but you can send it many.The variable in brackets (n) is the variable being sent and is of type longint. The :longint after the brackets is the type of variable to be returned.
ie function test(var1,var2:integer var3:byte) : byte would also be valid.
(note still only one variable- of type byte- being returned)
Since the function can be called from many different programmes that you might write in the future, we do not know in advance what the variable name will be, so we use anyname (n in this case)and replace it as it is called.
Eg fact(number),fact(varb),fact(goosh) are all valid funtion calls as long as they equate to an integer value.They are then assigned to n in the funtion. At the end of the function fact:=hold will return the vale to the original variable. n is known as a formal parameter and number, varb etc is known as the actual parameter.
That's as much as I can think of right now but I hope you get the general idea.
Don't worry about the number.2; it's just formatting syntax- it restricts the printed output to 2 spaces so the output looks good.
As for only being able to input to 33- you did well-are you using turbo pascal?? This is the overflow I was talking about in a previous post. You will probably have to go up to the next biggest vatiable size.
Hope there are not too many errors, good luck.
Elric


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Factorial in Pascal

Factorial in Pascal www.computing.net/answers/programming/factorial-in-pascal/7152.html

In Pascal keypressed, in ANSI C..?? www.computing.net/answers/programming/in-pascal-keypressed-in-ansi-c/13486.html

'Factorial' Numbers in VB6 www.computing.net/answers/programming/factorial-numbers-in-vb6/5520.html