Knowee
Questions
Features
Study Tools

int  func( int num) {      int a = num, r =0;      while (num > 0) {        r = r * 10 + num % 10;            num /= 10;      }     return r;                  }What will be the output for func(123) ?

Question

int  func( int num) {      int a = num, r =0;      while (num > 0) {        r = r * 10 + num % 10;            num /= 10;      }     return r;                  }What will be the output for func(123) ?

🧐 Not the exact question you are looking for?Go ask a question

Solution

The function func takes an integer as input and reverses its digits. Here's how it works:

  1. It initializes two variables, a and r, with a being equal to the input number and r being 0.
  2. It enters a while loop that continues as long as num is greater than 0.
  3. Inside the loop, it first multiplies r by 10 and then adds the last digit of num (obtained by num % 10).
  4. Then it removes the last digit from num by doing integer division by 10 (num /= 10).
  5. This process repeats until num becomes 0, effectively reversing the digits of the original number.
  6. Finally, it returns the reversed number.

So, if you call func(123), the function will return 321.

This problem has been solved

Similar Questions

int fun(int a,int b ){ if( !b ) return 1 int ans= fun(a,b/2) ans=ans*ans if( b%2 ) ans=(ans * a) return ans}What is output for fun ( 2,10 )?

What will be the output of the following program?#include<stdio.h>int main() {     int fun(int);     int i = fun(10);     printf("%d\n", --i);     return 0; } int fun(int i) { return (i++);}Select one:810119

What will be the output of the given program?int fun(int i){if(i==1 || i==0){return 1;}return fun(i-2)+fun(i-1);}void main(){int x=8;printf("%d", fun(x));}Options32303436

What will be the output of the given code?123456789101112#include <stdio.h> void fun(int *ptr){ *ptr = 30;}int main(){ int y = 20; fun(&y); printf("%d", y); return 0;}

What will be the output of the following code?int num = 2;int count=0;while(num!= 0){if ( num & 01)count ++;num >>=1;}cout<< count;return 0;

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.