Find the output of this program...only a few lines

Status
Not open for further replies.

sarincv

Journeyman
#include<stdio.h>
int fn(int);
int main(char argc,char **argv)
{

fn(3);

return 0;
}
int fn(int n)
{
int temp=0;
if(n>0)
{
temp = n + fn(--n);
}
printf("%d",temp);
return 0;
}

I got the ouput as 0012....can anyone explain to me why???

#include<stdio.h>
int fn(int);
int main(char argc,char **argv)
{

fn(3);

return 0;
}
int fn(int n)
{
int temp=0;
if(n>0)
{
temp = n + fn(n--);
}
printf("%d",temp);
return 0;
}


and what about this one........
 
Last edited:

Sykora

I see right through you.
For heaven's sake please format your code. It's virtually unreadable.

Code:
#include<stdio.h>

int fn(int);

int main(char argc,char **argv) {
    fn(3); 
    return 0;
}

int fn(int n) {
    int temp=0;
    if(n>0) {
        temp = n + fn(--n);
    }
    printf("%d",temp);
    return 0;
}

According to your recursive function,

3 >0 means temp = n + fn(--n) which is 2 + fn(2), becuase it decrements first, then evaluates.
2 > 0 means temp = 1 + fn(1)
1 > 0 means temp = 0 + fn(0)
0 = 0 means temp = 0

Since the only thing you're returning is 0, all the fn values are 0. And since if n is not > 0, temp is 0 anyway,

First it prints 0, because 0 = 0
Then it prints 0, because when n = 1, temp = 0 + 0 = 0
Then it prints 1, because when n = 2, temp = 1 + 0 = 1
Finally it prints 2, because when n = 3, temp = 2 + 0 = 2

EDIT : I didn't see the second one the first time around, but you can explain it by using the fact that n-- decrements _after_ it evaluates the expression.
 

Faun

Wahahaha~!
Staff member
Sykora said:
For heaven's sake please format your code. It's virtually unreadable.

Code:
#include<stdio.h>

int fn(int);

int main(char argc,char **argv) {
    fn(3); 
    return 0;
}

int fn(int n) {
    int temp=0;
    if(n>0) {
        temp = n + fn(--n);
    }
    printf("%d",temp);
    return 0;
}
According to your recursive function,

3 >0 means temp = n + fn(--n) which is 2 + fn(2), becuase it decrements first, then evaluates.
2 > 0 means temp = 1 + fn(1)
1 > 0 means temp = 0 + fn(0)
0 = 0 means temp = 0

Since the only thing you're returning is 0, all the fn values are 0. And since if n is not > 0, temp is 0 anyway,

First it prints 0, because 0 = 0
Then it prints 0, because when n = 1, temp = 0 + 0 = 0
Then it prints 1, because when n = 2, temp = 1 + 0 = 1
Finally it prints 2, because when n = 3, temp = 2 + 0 = 2
u did it first :D:D:D

recursive calls mean that the lowest layer will be shown first then last layer to be shown will be uppermost.

Why the hell return 0 ? try fibonacci sequence and towers of hanoi problem - thats mind twisting(later one)
 

fun2sh

Pawned!... Beyond GODLIKE
the second prog wil invoke n infinite loop. coz wen we use
Code:
n--
then first n value wil be pass then it wil be decremented. but the pass valu is 'n' itself so again f(n--) menas that that 'n' will be passed and thus it will enter the infinite loop.
 

Faun

Wahahaha~!
Staff member
fun2sh said:
the second prog wil invoke n infinite loop. coz wen we use
Code:
n--
then first n value wil be pass then it wil be decremented. but the pass valu is 'n' itself so again f(n--) menas that that 'n' will be passed and thus it will enter the infinite loop.

I though he posted the same code two times:confused:..lol my bad
 
Status
Not open for further replies.
Top Bottom