How can we find the output of recursion program in quicker way in c?


How can we find the output of recursion program in quicker way in c programming language


(1)What will be output of following c recursion program?

#include<stdio.h>
int main(){
    int x,num=4;
    x=call(num);
    printf("%d",x);
    return 0;
}
int call(int num){
    static int x=1,y;
    if(num>0){
         x=x*num;
         y=call(num-1)+call(num-2);
    }
    return x;
}

output: 48

(2)What will be output of following c recursion program?

#include<stdio.h>
int main(){
    int x,num=5;
    x=call(num);
    printf("%d",x);
    return 0;
}
int call(int num){
    static int x=1,y;
    if(num>0){
         x=x+num;
         y=call(num-2)+call(num-3);
    }
    return x;
}

Output: 12

(3)What will be output of following c recursion program?


#include<stdio.h>
int main(){
    int x,num=5;
    x=call(num);
    printf("%d",x);
    return 0;
}
int call(int num){
    static int x=1,y;
    if(num>0){
         x=x+num-1;
         y=call(num-1)+2;
    }
    return x;
}

Output: 11

(4)What will be output of following c recursion program?


#include<stdio.h>
int main(){
    int x,num=1;
    x=call(num);
    printf("%d",x);
    return 0;
}
int call(int num){
    static int x=1,y;
    if(num<4){
         x=x+num*2;
         y=call(num+1)+call(num+2)+call(num+3);
    }
    return x;
}

Output: 19

(5)What will be output of following c recursion program?


#include<stdio.h>
int main(){
    int x,num=1;
    x=call(num);
    printf("%d",x);
    return 0;
}
int call(int num){
    static int x=0,y;
    if(num<4){
         x=3*x+num;
         y=call(num+1)+2+call(num+3);
    }
    return x;
}

Output: 18

(6)What will be output of following c recursion program?


#include<stdio.h>
int main(){
    int x,num=1;
    x=call(num);
    printf("%d",x);
    return 0;
}
int call(int num){
    static int x=0;
    if(num<4){
         x=2*x-num;
         call(num+1);
         call(num+2);
    }
    return x;
}

Output: -25

1 comment:

krishna kumar said...

how its work i can't understood