3,799 views
0 votes
0 votes

What does the following function print for n = 25?

void fun(int n)

{

  if (n == 0)

    return;

 

  printf("%d", n%2);

  fun(n/2);

1 Answer

1 votes
1 votes

Notes '%' operator returns the reminder after the division.
Can also be seen as returning the last bit of the number in our case.

It prints 10011 because:

fun(25)
print 25%2=1

    fun(25/2=12)
    print 12%2=0

        fun(12/2=6)
        print 6%2=0

             fun(6/2=3)
                 print 3%2=1

                 fun(3/2=1)
                     print 1%1=1

Related questions

79
views
1 answers
1 votes
harshitraj12 asked 5 days ago
79 views
Find Output of below Code#include<stdio.h>int sum(int n) { n = 3; if (n == 2) return 2; return sum(n-1)*n ;}int main() { printf("%d\n", sum(5) ); return 0}
268
views
2 answers
0 votes
Debargha Mitra Roy asked Apr 10
268 views
What is the output of the below code?#include <stdio.h> void main() { static int var = 5; printf("%d ", var--); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1.3k
views
3 answers
3 votes
Laxman Ghanchi asked May 19, 2023
1,278 views
#include<stdio.h> void print(int n) { printf("Hello "); if(n++ == 0) return ; print(n); n++; } int main() { void print(); print(-4); }How many times printf execute?? And How ??
773
views
1 answers
0 votes
Laxman Ghanchi asked May 19, 2023
773 views
#include<stdio.h>void print(int n){ printf("Hello "); if(n++ == 0) return ; print(n); n++;}int main(){ void print(); print(-4);}