Question

What is the output of the following c program. Why, Can you tell me how do you solve it?

#include <stdio.h> int fun(int n) { if(n==0) { return 1; } else return 7 + fun(n-2); } int main() { printf(%d, fun (4)); re

0 0
Add a comment Improve this question Transcribed image text
Answer #1

THE GIVEN SOURCE CODE IS:

#include<stdio.h>
int fun(int n)
{
if(n==0) {
return 1;
}
else
return 7 + fun(n-2);
}
int main(){
printf("%d",fun(4));
return 0;
}

THE OUTPUT OF THE ABOVE PROGRAM WILL BE: 15

THE SCREENSHOT OF THE OUTPUT IS BELOW:

(base) mouli Lenovo-G580:$ gcc sc.C (base) mouli@Lenovo-G580:~$ ./a.out 15(base) mouli Lenovo-G580:~$

DETAILED EXPLANATION:

IN THE main() WE CALL fun(4).

THEN IT WILL SEARCH FOR fun() OUTSIDE A main().

IF fun() FOUND THEN IT WILL EXECUTE ALL THE INTRUCTIONS INSIDE IT.

cases:

fun(4):

here n!=0 so the return value is 7+fun(4-2) //output=7

fun(2):

here n!=0 so the return value is 7+fun(2-2) //output=7+7

fun(0):

here n=0 so return value is 1 //output=7+7+1=15.

I HOPE THIS WILL HELP YOU.........

Add a comment
Know the answer?
Add Answer to:
What is the output of the following c program. Why, Can you tell me how do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT