edited by
483 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

653
views
0 answers
0 votes
air1ankit asked Jul 12, 2018
653 views
que--Study the following program written in a block structured language.var p, q : integer;begin p:= p+q; q:= p−q; p:= p−q;end;(a) ... (d) leaves (p) and (q) unchanged
764
views
1 answers
1 votes
Robin Hoque asked Aug 18, 2017
764 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;}(A)2,1,2(B)2,1,1(C)2,1,4(D)2,2,8
6.6k
views
1 answers
0 votes
Harikesh Kumar asked Jun 3, 2017
6,580 views
If the address of A[1][1] and A[2][1] 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. matrix majorAnswer is row major explain how ?
350
views
0 answers
0 votes
Rahul_Rathod_ asked Dec 28, 2018
350 views
let R be the class of recursive program and l be the class of iterative program now consider below statement S S : every program in R uses strictly more space compare to its equivalent program in class ITRUE of FALSE ?