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) ?
Solution
The function func takes an integer as input and reverses its digits. Here's how it works:
- It initializes two variables,
aandr, withabeing equal to the input number andrbeing 0. - It enters a while loop that continues as long as
numis greater than 0. - Inside the loop, it first multiplies
rby 10 and then adds the last digit ofnum(obtained bynum % 10). - Then it removes the last digit from
numby doing integer division by 10 (num /= 10). - This process repeats until
numbecomes 0, effectively reversing the digits of the original number. - Finally, it returns the reversed number.
So, if you call func(123), the function will return 321.
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;
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.