edited by
461 views
0 votes
0 votes

Consider the following C code snippets, labeled as P1, P2 and P3, the output of P1 is “X”, of P2 is “Y” and of P3 as “Z”. What is the value of “X+Y+Z”

P1
#include <stdio.h>

int f(int n)
{
    static int r=40;
    if (n==0 || n==1) return 0;
    if (n%2==0)
    {
        r=r-4;
        return f(n-1)-r;
    }
    else return f(n-1)+(2*r);
}
int main() 
{
    printf("%d",f(6));
    return 0;
}

 

P2
#include <stdio.h>

int f(int n)
{
    int r=40;
    if (n==0 || n==1) return 0;
    if (n%2==0)
    {
        r=r-4;
        return f(n-1)-r;
    }
    else return f(n-1)+(2*r);
}
int main() 
{
    printf("%d",f(6));
    return 0;
}

 

P3
#include <stdio.h>

int f(int n,int r)
{
    
    if (n==0 || n==1) return 0;
    if (n%2==0)
    {
        r=r-4;
        return f(n-1,r)-r;
    }
    else return f(n-1,r)+(2*r);
}
int main() 
{
    printf("%d",f(6,40));
    return 0;
}

 

edited by

1 Answer

Related questions

1 votes
1 votes
1 answer
2
Robin Hoque asked Aug 18, 2017
739 views
What is the output of following program#include<stdio.h>int main(){ short int i=20; char c=97; printf("%d,%d,%d\n",sizeof(i),sizeof(c),sizeof(c+i)); return 0;...
0 votes
0 votes
1 answer
3
Harikesh Kumar asked Jun 3, 2017
6,407 views
If the address of A and A are 1000 and 1010 respectively and each element occupies 2 byte then the array has been stored in which order ?1.row major 2.column major 3....
3 votes
3 votes
1 answer
4
Ravi prakash pandey asked Nov 12, 2017
660 views
what wil be sizeof(void)and sizeof(void *)