in Programming in C retagged by
10,846 views
23 votes
23 votes

Consider the following $\text{C}$ program:

#include<stdio.h>

int counter=0;

int calc (int a, int b) {
        int c;
        counter++;
        if(b==3) return (a*a*a);
        else {
                c = calc(a, b/3);
                return (c*c*c);
        }
}

int main() {
        calc(4, 81);
        printf("%d", counter);
}

The output of this program is ______.

in Programming in C retagged by
by
10.8k views

3 Comments

Anyone know how to see the output of what the calc() function is returning??

I know it will be very huge number but even if I change to long long int then also not able to see output. just wanted to know is there is any facality in C to see that much huge no. below is my code feel free to edit and let me know if you are able to see output. cause it is outputing 0 and i think this will going off the double doube int max limit.

https://ideone.com/H3FCkD
 

0
0
Just count the number of digits in the result. And a double value can hold how many digits?
1
1
thanks this works...🙂
0
0

5 Answers

41 votes
41 votes
Best answer
int main() {
        calc(4, 81);
        printf("%d", counter);
}
printf("%d", counter);
So we need only counter value.
Each function increments counter value by 1. Goal is to find the number of function calls.
Squence of function calls:
calc(4, 81) ---> calc(4, 27) ---> calc(4, 9) ---> calc(4, 3) ---> return

4 function calls.
counter = 4
edited by

4 Comments

For given question calc(4,81) will return 4^81 to main().

 

And if in question return(c+c+c) then calc(4,81) will return 1728 to main(). And variable c local or global doesn’t matter(same output 1728). @Abhineet Singh

https://ideone.com/dBkD3P

 

0
0
Yes it is returning 4^81
1
1

@HHH777

by default function is integer only so no error.

0
0
8 votes
8 votes

There is no need to calculate return value we just need value of counter.

So answer is 4.

1 comment

why it's return value is displaying 0? shouldn't it be 64?
0
0
3 votes
3 votes
#include<stdio.h>

int counter=0;

int calc (int a, int b) {
        int c;
        counter++;
        if(b==3) return (a*a*a);
        else {
                c = calc(a, b/3);
                return (c*c*c);
        }
}

int main() {
        calc(4, 81);
        printf("%d", counter);
}

Here counter is a global variable, you can use anywhere of the program, 

 Now when main() call the calc() by the value (4,81) 

--> counter=counter+1 so now counter=1.

Now for 81 not eq to 3 so else block got executed and Now b=81/3=27.

Again counter increased by 1 and now counter =2.

In this way when b=3 counter will become 4 and cacl() return 64;

counter=3

int calc(4,3)

{

int c;

counter++;

if(b==3) return (a*a*a);
        else {
                c = calc(a, b/3);
                return (c*c*c);
        }

}

So, ans is 4.

2 votes
2 votes
The main function is just printing counter value. There's no need to calculate the values of return statements in the function calc().

 

Therefore , Answer is 4.
Answer:

Related questions