how to let 125 become 521
Write your question here.
how to cin 125(one hundred and twenty five),cout 521 (five hundred and twenty one).
1.
1 2 3
|
int a;
cout << "Enter 125 : ";
cin >> a; // Enter 125 from console
|
2.
cout << 521;
@SakurasouBusters: I think the OP may want to know how to reverse any given integer. If the program only printed reversed 125, it'd pretty useless.
@ll1234: To reverse an Integer, there are several ways to do it.
Easiest I found is simply turn it into a String or array of char and then reverse the order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <string>
#include <iostream>
int main()
{
std::string input;
std::cout << "Enter a number: ";
std::cin >> input;
for( int i = input.size() - 1 ; i >= 0 ; i-- )
{
std::cout << input[i];
}
}
|
Last edited on
And if you want 521 as an int..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string input;
std::stringstream ss;
int reverse_num;
std::cout << "Enter a number: ";
std::cin >> input;
std::reverse(input.begin(), input.end());
ss << input;
ss >> reverse_num;
std::cout >> reverse_num;
}
|
Just make sure that input is digit.
Topic archived. No new replies allowed.