Hi there, I'm am trying to write a recursive C++ function that writes the digits of a positive decimal integer in reverse order.
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <math.h>
#include <string>
using std::string;
using namespace std;
int reverse_num (int number, int m) ;
int main()
{
int number;
int m=0;
cout << "Enter number to reverse :";
cin >> number;
cout << reverse_num(number,0) << endl; //calling the function
system("pause") ;
return 0;
}
int reverse_num(int number,int m)
{
if(number==0)
return m; //base (exit condition)
m*=10;
m+=number%10;
return reverse_num(number/10,m); //recursive function called here
}
I'm compiling the code in Visual C++, so far the code can only reverse integers and I haven't a notion on how to keep the numbers after the decimal point. I'm not even sure if the number after the decimal point should exist! Should, for example, 27.3 become 72 or 372? Any advice would be much appreciated!