in Programming in C retagged by
10,883 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.9k 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

15 Comments

sir,

here value of calc(4,81) is not assigned to a variable but the return type of calc is int. so will this prgm give an error?
1
1
No, It will not give error because counter is global variable.
0
0
edited by

@Digvijay Pandey Sir

#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() {
        long int k;
        k=calc(4, 81);
        printf("%d", counter);
        printf("%d",k);
}

counter $=4$

What is the value of $k?$

I think $k=64*64*64=262,144$

please correct me, if I'm wrong?

1
1
$calc(4,3) = 4^3$

$calc(4,9) = (4^3)^3 = 2126244$

$calc(4,27) = ((4^3)^3)^3$

$calc(4,81) = (((4^3)^3)^3)^3$

This will result in overflow
15
15
Yeah, that's right.
1
1
It can be printed with $\mathbf{long\;long}$ I suppose.
1
1

@Mk Utkarsh can you check once again.. i am getting $calc(4,81)$ as $4^{27}$

0
0

how? show

0
0

@Mk Utkarsh

calc(4,3) returns $4^{3}$  to calc(4,9)... calc(4,9) returns its cube i.e $4^{9}$ is  to calc(4,27)... calc (4,27) returns its cube i.e. $4^{27}$ to calc(4,81)..

0
0

 Correct and what does calc(4,81) return?

0
0
what would be the value of c if we modify it to return(c+c+c) and ofcourse c is made global. I’m confused between 576 and 1728...
0
0
@Mk Utkarsh, I ran the program and checked. It is giving 4^27 only, I also think it should give 4^81...
1
1

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