0
How to represent a number that we read from the end?For example : 134 how to represent it as 431?
8 ответов
+ 3
You could convert the integer to a string and then reverse the string
+ 1
It is just an example, you can use a variable of any name you want
+ 1
Xx Yyy If you are asking about my explanation, then I used num as input instead of your x so it just x = num. Now and it's one of previous answer copy pasted.
rev is the variable name which hold the reverse value of input at end of loop.
so num => x = 234,
rev = 432
If you want to strings way, use
s = to_string(x) , now c++ have string reversing function. You can use.
Or you can use a loop to reverse it.
0
You can get a last digit for a number by num%10 so that digit will be first digit of reverse number.., you can add further digits by rev*10 + num%10..
using a loop :
Ex:
234
234%10=4, rev=0*10+4=4, num=234/10=23
23%10=3, 4*10+3=43, 23/10=2
2%10=2, 43*10+2=432, 2/10=0
Now you can stop loop now since num=0
Hope it helps...
0
Sorry but I didn't understand exactly what do you mean because in my code I'm working with x that the user will give its value (using cin) so I don't know its character that why I want to represent it in general as x.
Hope you understood me.
0
Ok thank you so much for your answers
0
Hey, when you talked about the string way, how can I declare x and s, I mean are they int or char..?
0
It depends on your input.
Ex:
int x = 234;
string s = to_string(x) ;
//reverse string now..
Else
string s = "234";
Reverse string now..
Use stoi() function to convert to int type..
Ex:
int rev = stoi("432");
Xx Yyy Add your try if something not work..