0

How to represent a number that we read from the end?For example : 134 how to represent it as 431?

17th Feb 2022, 8:38 PM
Basma M
8 odpowiedzi
+ 3
You could convert the integer to a string and then reverse the string
17th Feb 2022, 8:42 PM
Lisa
Lisa - avatar
+ 1
It is just an example, you can use a variable of any name you want
18th Feb 2022, 12:31 PM
Lisa
Lisa - avatar
+ 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.
18th Feb 2022, 1:10 PM
Jayakrishna 🇮🇳
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...
18th Feb 2022, 9:09 AM
Jayakrishna 🇮🇳
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.
18th Feb 2022, 12:10 PM
Basma M
0
Ok thank you so much for your answers
2nd Mar 2022, 6:33 PM
Basma M
0
Hey, when you talked about the string way, how can I declare x and s, I mean are they int or char..?
5th Mar 2022, 11:57 AM
Basma M
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..
5th Mar 2022, 12:41 PM
Jayakrishna 🇮🇳