Write a C++ program to reverse a number using a user defined function.
Implementation of the above problem in C++:
#include<iostream>
#include<math.h>
using namespace std;
void reverse(int n)
{
int a;
cout<<"\nReverse output without return type is = ";
while(n!=0)
{
a=n%10;
cout<<a;
n=n/10;
}
}
int reverse1(int n)
{
int a,c=0,x,z,b[100],i=0;
while(n!=0)
{
b[i]=n%10;
i++;
x=i;
n=n/10;
}
int y=x-1;
for(i=0; i<=x-1;i++)
{
z=b[i]*pow(10,y);
c=c+z;
y--;
}
return c;
}
int main()
{
int n;
cout<<"\nEnter the number to be reversed \n";
cin>>n;
reverse(n);
cout<<"\nReverse output with return type is = "<<reverse1(n);
return 0;
}
The output of the above program is tested on www.jdoodle.com
Output:
Enter the number to be reversed
234
Reverse output without return type is = 432
Reverse output with return type is = 432
Thanks
Mukesh Rajput
Post A Comment:
0 comments: