on Leave a Comment

C++ Program to Reverse Number

/*C++ Program to Reverse Number*/
#include<iostream>
using namespace std;
int main()
{
    int n,rem,rev=0;
    cout<<"Enter a number : ";
    cin>>n;
    while(n!=0)
    {
        rem=n%10;
        rev=rev*10+rem;
        n=n/10;
    }
    cout<<"Reverse number is "<<rev;
    return 0;
}

OUTPUT:

Enter a number : 58
Reverse number is 85

/*C++ Program to Reverse Number using Constructor*/
#include<iostream>
using namespace std;
class REV
{
public:
    REV(int n)
    {
        cout<<"Original number is "<<n;
        int rem,rev=0;
        while(n!=0)
            {
                 rem=n%10;
                 rev=rev*10+rem;
                 n=n/10;
            }
        cout<<endl<<"Reverse number is "<<rev;
    }
};
int main()
{
    int n;
    REV x(678);
    return 0;
}

OUTPUT:

Original number is 678
Reverse number is 876


0 comments:

Post a Comment