+5 votes
79 views

Consider the code below, defining the functions $f$ and $g$:

f(m, n) {
if (m == 0) return n;
else {
q = m div 10;
r = m mod 10;
return f(q, 10*n + r);
}
}
g(m, n) {
if (n == 0) return m;
else {
q = m div 10;
r = m mod 10;
return g(f(f(q, 0), r), n-1);
}
}

What does $g(m, n)$ compute, for nonnegative numbers $m$ and $n$?

| 79 views

## 1 Answer

0 votes
#include<iostream>
using namespace std;
int g(int m, int n);
int f(int m, int n);
int main()
{
int m,n;
m=4321;
n=4;
cout<<g(m,n);
}

int f(int m, int n) {
int q,r;
if (m == 0) return n;
else {
q = m /10;
r = m %10;
return f(q, 10*n + r);
}
}
int g(int m, int n)
{
int q,r;
if (n == 0)
return m;
else {
q = m/10;
r = m%10;
return g(f(f(q, 0), r), n-1);
}
}


it will return same number let m=4321,n=4 //Number or digits in m first f() call will reverse then again reverse so it will be same number

by Active (2.1k points)

+2 votes
1 answer
1
+2 votes
0 answers
2
+4 votes
1 answer
3
+2 votes
0 answers
4
+14 votes
1 answer
5
+2 votes
0 answers
6
+3 votes
1 answer
7