What will be the output of the given program?int fun(int x){int b;if(x==1 || x==0)return 1;elseb=x*fun(x-2);return b;}void main(){int x=6;printf("%d", fun(x));}Options42483640
Question
What will be the output of the given program?int fun(int x){int b;if(x==1 || x==0)return 1;elseb=x*fun(x-2);return b;}void main(){int x=6;printf("%d", fun(x));}Options42483640
Solution
The output of the given program will be 48.
Here's the step by step explanation:
-
The function
fun()is a recursive function that multiplies the inputxwith the result of the function called withx-2untilxis 1 or 0. Whenxis 1 or 0, it returns 1. -
In the
main()function,fun()is called withxas 6. -
The function
fun()is called as follows:fun(6)returns6 * fun(4)=6 * 24=144fun(4)returns4 * fun(2)=4 * 2=8fun(2)returns2 * fun(0)=2 * 1=2fun(0)returns1
-
Therefore, the final output of the program is
144, not48.
Please note that there seems to be a mistake in the options provided, as none of them match the correct output.
Similar Questions
What will be the output of the following program?#include<stdio.h>int main() { int fun(int); int i = fun(10); printf("%d\n", --i); return 0; } int fun(int i) { return (i++);}Select one:810119
What will be the output of the given code?123456789101112#include <stdio.h> void fun(int *ptr){ *ptr = 30;}int main(){ int y = 20; fun(&y); printf("%d", y); return 0;}
What is the output of following program?# include <stdio.h>void fun(int x){ x = 30;}int main(){ int y = 20; fun(y); printf("%d", y); return 0;}a.30b.20c.Compile errord.Run error
What will be the output of the following code snippet?123456#include <stdio.h>int main() { int a = 4, b = 7; printf("%d", a & b); return 0;}
What will be the output of the following C code #include <stdio.h> int main() { int x = 20; x %= 3; printf("%d",x); return 0; }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.