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
    #1
    1. Advertising

  2. jacob navia Guest

    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
    #2
    1. Advertising

  3. Vladimir Oka Guest

    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
    #3
  4. Guest

    Yeah I did it. Can you solve that trick?
    , May 16, 2006
    #4
  5. Vladimir Oka Guest

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


    Did what? What trick? Quote context. Read
    <http://cfaj.freeshell.org/google/>.

    In response to the above:

    - Good for you.
    - Yes I can.

    See also: Jacob Navia's post.
    Vladimir Oka, May 16, 2006
    #5
  6. 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

    Giannis Papadopoulos
    Computer and Communications Engineering dept. (CCED)
    University of Thessaly
    http://dop.freegr.net/
    Giannis Papadopoulos, May 16, 2006
    #6
  7. 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
    #7
  8. CBFalconer Guest

    "" 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/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
    CBFalconer, May 16, 2006
    #8
  9. 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
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S2VubnkgTS4=?=

    interesting fact about date.

    =?Utf-8?B?S2VubnkgTS4=?=, Jun 11, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    330
  2. comcast
    Replies:
    5
    Views:
    400
    giganews
    Mar 3, 2004
  3. Generic Usenet Account

    A not very well publicized fact about C++ templates

    Generic Usenet Account, Dec 21, 2003, in forum: C++
    Replies:
    7
    Views:
    369
    Ron Natalie
    Dec 22, 2003
  4. Schnoffos
    Replies:
    2
    Views:
    1,190
    Martien Verbruggen
    Jun 27, 2003
  5. Hal Styli
    Replies:
    14
    Views:
    1,605
    Old Wolf
    Jan 20, 2004
Loading...

Share This Page