Answer :
Answer:
#include <iostream>
using namespace std;
int main() {
int n, reversedNumber = 0, remainder;
cout << "Enter a number: ";
cin >> n;
int length = to_string(n).length();
if (length == 4){
while(n != 0) {
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
cout << "Reversed Number = " << reversedNumber << endl;
}
else{
cout << "Invalid Input!" << endl;
}
return 0;
}
Explanation:
So, it's pretty easy. It finds the remainder to reverse the number and for the invalid input part, you convert the input into a string and then use .length() to find the length. Then you can reverse the number and show whatever output
Answer:
#include <iostream>
using namespace std;
int main() {
int n, reversedNumber = 0, remainder;
cout << "Enter a number: ";
cin >> n;
int length = to_string(n).length();
if (length == 4){
while(n != 0) {
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
cout << "Reversed Number = " << reversedNumber << endl;
}
else{
cout << "Invalid Input!" << endl;
}
return 0;
}
Explanation:
So, it's pretty easy. It finds the remainder to reverse the number and for the invalid input part, you convert the input...
Explanation: