int fact( int n )

V

venkatesh.k.desai5

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;
}
 
J

jacob navia

(e-mail address removed) 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
 
V

Vladimir Oka

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>
 
G

Giannis Papadopoulos

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

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

Richard Heathfield

(e-mail address removed) 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)?
 
C

CBFalconer

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/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
C

Clever Monkey

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top