Knowee
Questions
Features
Study Tools

Output of following C program is:-int fun(int x) {  int k = 1;  if(x <= 1)    return 1;  k = fun(x-2)* fun(x - 1);    return k;} void main() {  printf("%d",fun(4));}a.1b.Segmentation faultc.10d.6

Question

Output of following C program is:-int fun(int x) {  int k = 1;  if(x <= 1)    return 1;  k = fun(x-2)* fun(x - 1);    return k;} void main() {  printf("%d",fun(4));}a.1b.Segmentation faultc.10d.6

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

Solution

To determine the output of the given program, let's analyze the code step by step:

  1. The program defines a function named "fun" that takes an integer parameter "x".
  2. Inside the "fun" function, a local variable "k" is initialized to 1.
  3. The function checks if the value of "x" is less than or equal to 1. If true, it returns 1.
  4. If the condition in step 3 is false, the function recursively calls itself with the arguments "x-2" and "x-1".
  5. The result of the recursive calls is multiplied and stored in the variable "k".
  6. Finally, the function returns the value of "k".

In the "main" function:

  1. The program calls the "fun" function with the argument 4.
  2. The result of the "fun" function is printed using the "printf" function.

Based on the analysis, the output of the program will be the value returned by the "fun" function when called with the argument 4.

To determine the exact output, we need to evaluate the function "fun(4)".

fun(4) = fun(2) * fun(3)

To evaluate fun(2): fun(2) = fun(0) * fun(1)

To evaluate fun(0): fun(0) = 1

To evaluate fun(1): fun(1) = 1

Substituting the values back: fun(2) = 1 * 1 = 1

To evaluate fun(3): fun(3) = fun(1) * fun(2)

Substituting the values back: fun(3) = 1 * 1 = 1

Substituting the values back: fun(4) = 1 * 1 = 1

Therefore, the output of the program will be 1.

The correct answer is: a. 1

This problem has been solved

Similar Questions

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

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

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

Consider the C program shown below -int sumOdd(int *a, int n){  if(n <= 0) return 0;  else if (*a % 2 != 0) return *a + sumOdd (a + 1, n - 1);  return sumOdd (a + 1, n - 1);}int main(){  int a[] = {1,2, 3, 4, 5 ,6};  printf("%d\n", sumOdd(a, 6));  return 0;}The output of this program is -912158

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.