# int fact( int n )

Discussion in 'C Programming' started by venkatesh.k.desai5@gmail.com, May 16, 2006.

1. ### Guest

Hi All,

I want to know how many times 'Hi' will prints; and why?

#include<stdio.h>

int fact( int n );

main() {

int f;

f = fact( 4 );

printf("\nfact = %d", f);
}

int fact( int n ) {
int fa;

if ( n == 0 ) return 1;

fa = n * fact( n - 1 );

printf("\nHi");

return fa;
}

, May 16, 2006

2. ### jacob naviaGuest

a écrit :
> Hi All,
>
> I want to know how many times 'Hi' will prints; and why?
>
>
> #include<stdio.h>
>
> int fact( int n );
>
> main() {
>
> int f;
>
> f = fact( 4 );
>
> printf("\nfact = %d", f);
> }
>
> int fact( int n ) {
> int fa;
>
> if ( n == 0 ) return 1;
>
> fa = n * fact( n - 1 );
>
> printf("\nHi");
>
> return fa;
> }
>

Do your own homework.

Or give us the address of your teacher. We will send him/her the
solution directly

jacob navia, May 16, 2006

3. ### Vladimir OkaGuest

wrote:
> I want to know how many times 'Hi' will prints; and why?

The first one is easy: just compile and run the code.
The second one is more tricky, as it involves exerting some actual
effort in learning about C.

<snip textbook code>

Vladimir Oka, May 16, 2006
4. ### Guest

Yeah I did it. Can you solve that trick?

, May 16, 2006
5. ### Vladimir OkaGuest

wrote:
> Yeah I did it. Can you solve that trick?

Did what? What trick? Quote context. Read

In response to the above:

- Good for you.
- Yes I can.

Vladimir Oka, May 16, 2006
6. ### Giannis PapadopoulosGuest

wrote:
> Hi All,
>
> I want to know how many times 'Hi' will prints; and why?
>
>
> #include<stdio.h>
>
> int fact( int n );
>
> main() {
>
> int f;
>
> f = fact( 4 );
>
> printf("\nfact = %d", f);
> }
>
> int fact( int n ) {
> int fa;
>
> if ( n == 0 ) return 1;
>
> fa = n * fact( n - 1 );
>
> printf("\nHi");
>
> return fa;
> }
>

Better write it like this:

#include <stdio.h>

int fact(int n);

int main(void) {
int f;

f = fact(4);
printf("\nfact = %d\n", f);

return 0;
}

int fact(int n) {
int fa;

if (n==0) return 1;
fa = n*fact(n-1);
printf("\nHi");

return fa;
}

1) Declared main() as int main(void) and
2) added a '\n' to the last printf() to ensure that everything will be
printed.

You can now run the algorithm in piece of paper...

--
one's freedom stops where others' begin

Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/

Giannis Papadopoulos, May 16, 2006
7. ### Richard HeathfieldGuest

said:

> Hi All,
>
> I want to know how many times 'Hi' will prints; and why?

Too many, because you misimplemented your function to be recursive - a poor
way to calculate a factorial.

Also, what happens when you call fact(-4) instead of fact(4)?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Richard Heathfield, May 16, 2006
8. ### CBFalconerGuest

"" wrote:
>
> Yeah I did it. Can you solve that trick?

I'm glad you did. Did she like it? What does that have to do with
the C language?

In general on usenet you should realize that readers may very well
not have convenient access to previous articles in a thread. That
means that your reply articles should include adequate context, so
that they stand by themselves. Google is NOT usenet, it is only a
very poor interface to the real usenet system. To include proper
context when using google, see my sig. below. Please be sure to
read the referenced URLs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

CBFalconer, May 16, 2006
9. ### Clever MonkeyGuest

wrote:
> Yeah I did it. Can you solve that trick?
>

It's obvious that the magician simply moved the table to push the coin
through a hole. This, and slight of hand, made the coin *appear* to
pass into the glass and through the table.

Clever Monkey, May 16, 2006