898 views
2 votes
2 votes

Question 

Consider the following C-program 
void foo (int n, int sum) { 
int k = 0, j = 0; 
if (n = = 0) return; 
k = n% 10; j = n/10; 
sum = sum + k; 
foo (j, sum); 
printf (“%d”, k); 

int main ( ) { 
int a = 2048, sum = 0; 
foo (a, sum); printf (“%d/n”, sum); 

What does the above program print?

  • A    8, 4, 0, 2, 14
  • B    8, 4, 0, 2, 0
  • C    2, 0, 4, 8, 14
  • D    2, 0, 4, 8, 0

Please give step by step explanation?

1 Answer

0 votes
0 votes

B is correct option.

1.main()

a=2048

sum =0

2.foo(2048,0)

n=2048

Sum=0

K= 8 j= 204

Sum= 8

3.foo(204,8)

n= 204 sum= 8

K=4 j=20

sum= 12

4.foo(20,12)

​​​​n=20 sum=12

K= 0 j= 2

Sum = 12

5.foo(2,12)

n=2 sum 12

K= 2 j=0

Sum =14

6.foo(0,14)

return to 5

In 5 print k =2 then return to 4

In 4 print k=0 then return to 3

In 3 print k = 4 then return to 2

In 2 print k =8 then return to 1. i-e main()

In main sum variable remains 0

As only value of sum variable was passed

Hence sum =0

Will be printed!

Related questions

2 votes
2 votes
1 answer
1
rupamsardar asked Aug 30, 2023
495 views
#include <stdio.h int f(int x) { if(x%2==0) { return f(f(x-1)); } else return (x++); } int main() { printf("%d",f(12)); ret...
0 votes
0 votes
1 answer
2
SHWETAV SUMAN asked Oct 6, 2022
334 views
i have typed the following code but when i executed it the solution was not according to my expectation.unsigned short int y= -9; int iy=y; printf(“%d”,iy); solutio...
0 votes
0 votes
3 answers
3
ykrishnay asked May 30, 2022
1,071 views
int main(){extern int a = 20;printf("%d",a); }//why this gives error but followig code is not extern int a =20;int main(){printf("%d",a); } //why this happens that first ...
0 votes
0 votes
0 answers
4
ykrishnay asked May 20, 2022
386 views
What is stream in c programming ? as i read file io in c programming so thats where i read “stream” word so what stream means ? thank you