Knowee
Questions
Features
Study Tools

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

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the given program will be 48.

Here's the step by step explanation:

  1. The function fun() is a recursive function that multiplies the input x with the result of the function called with x-2 until x is 1 or 0. When x is 1 or 0, it returns 1.

  2. In the main() function, fun() is called with x as 6.

  3. The function fun() is called as follows:

    • fun(6) returns 6 * fun(4) = 6 * 24 = 144
    • fun(4) returns 4 * fun(2) = 4 * 2 = 8
    • fun(2) returns 2 * fun(0) = 2 * 1 = 2
    • fun(0) returns 1
  4. Therefore, the final output of the program is 144, not 48.

Please note that there seems to be a mistake in the options provided, as none of them match the correct output.

This problem has been solved

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

1/3

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.