Write a C++ program to find Reverse of a Number using class.inputEnter a number: 12345output54321
Question
Write a C++ program to find Reverse of a Number using class.
Input
Enter a number: 12345
Output
54321
Solution
Sure, here is a simple C++ program that uses a class to reverse a number:
#include<iostream>
using namespace std;
class ReverseNumber {
int num, rev;
public:
ReverseNumber() : num(0), rev(0) {}
void input() {
cout << "Enter a number: ";
cin >> num;
}
void reverse() {
while(num != 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}
}
void display() {
cout << "Reversed Number: " << rev << endl;
}
};
int main() {
ReverseNumber rn;
rn.input();
rn.reverse();
rn.display();
return 0;
}
In this program, we have a class ReverseNumber
with two data members num
and rev
. The input()
member function is used to take the number as input from the user. The reverse()
member function is used to reverse the number. The display()
member function is used to display the reversed number. In the main()
function, we create an object of the class and call these functions.
Similar Questions
Write a C++ program to find Reverse of a Number using class.inputEnter a number: 12345output54321
Write a program in C to print a string in reverse using a character pointer.Also print the uppercase of the first letter of the reversed word.
Write a C++ program to check if a given number is a palindrome. A palindrome is a number that remains the same when its digits are reversed.
Reverse last k digits of the given numberInput Format:Accept two integer as inputOutput Format:print the reversed k digits
find the output of given code.#include <stdio.h>int main(){int a,b=110,c=20;printf("%d",a=b-=c*=5);return 0;}-1045010110
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.