+ 2

How would you reverse a number

// From int target = 12345; // To int result = 54321; Any answer is appreciated 👍

28th Aug 2021, 5:46 AM
Tim
Tim - avatar
14 Réponses
+ 5
Delicate Cat Normally I would do this but it looks amateur and I'm pretty sure the performance is awful, which is why I didn't care to show my attempt 😉 https://code.sololearn.com/cR8e68JxvGqS/?ref=app
28th Aug 2021, 6:48 AM
Tim
Tim - avatar
+ 4
https://www.c-sharpcorner.com/blogs/reverse-a-number-and-string-in-c-sharp1
28th Aug 2021, 5:55 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 3
int target = 12345; char[] temp = target.ToString().ToCharArray(); Array.Reverse(temp); int result = Int32.Parse(String.Join("",temp)); Console.WriteLine(result);
28th Aug 2021, 6:09 AM
CodeSmith
CodeSmith - avatar
+ 3
Ugh, nvm, I don't know how to reverse it the other way around
28th Aug 2021, 6:52 AM
Tim
Tim - avatar
+ 3
Andy_Roid Thank you, Andy
28th Aug 2021, 7:01 AM
Tim
Tim - avatar
28th Aug 2021, 6:59 AM
CodeSmith
CodeSmith - avatar
+ 2
your method would have worked, but u need to start from the last element to first element, you can do that by using for loop. https://code.sololearn.com/cOKUlp6xN5E6/?ref=app
28th Aug 2021, 7:05 AM
CodeSmith
CodeSmith - avatar
+ 2
https://code.sololearn.com/cPq5qjskrdKH/?ref=app hope this helps u
28th Aug 2021, 9:05 AM
Vtec Fan
Vtec Fan - avatar
+ 2
//In C language #include<stdio.h> void main() { int target = 12345, result = 0, r; while(target > 0) { r = target % 10; result = (result * 10) + r; target /= 10; } printf("%d", result); } Hope its easier for you convert into c#
29th Aug 2021, 4:32 PM
Abirami
Abirami - avatar
+ 2
Kartik Singh Negi Very detailed and comprehensible, thanks for putting much effort into this 😉
29th Aug 2021, 6:50 PM
Tim
Tim - avatar
+ 1
Rushikesh Thank you 😀
28th Aug 2021, 12:07 PM
Tim
Tim - avatar
+ 1
int target =12345; int rem=0, rev=0 ,result=0; while(target!=0) { rem=target%10; rev=rev*10+rem; target=target/10; } result=rev; To reverse a number we need to get last number at first second last at second ....... In loop //rem =5 //rev=0+5=5 //target =1234 Again // rem=4 //rev=5*10+4=54 //target=123 Again //rem=3 //rev=54*10+3=543 //target=12 Again //rem=2 //rev=543*10+2=5432 //target=1 Again //rem=1 //rev=5432*10+1=54321 //target=0 And ends because target becomes 0
29th Aug 2021, 5:12 PM
Kartik Singh Negi
Kartik Singh Negi - avatar
+ 1
Abirami Thanks for the code 👍
29th Aug 2021, 6:48 PM
Tim
Tim - avatar
+ 1
We can reverse a number in C# using loop and arithmetic operators. In this program, we are getting number as input from the user and reversing that number. Let's see a simple C# example to reverse a given number. using System;     public class ReverseExample      {        public static void Main(string[] args)         {          int  n, reverse=0, rem;                   Console.Write("Enter a number: ");              n= int.Parse(Console.ReadLine());             while(n!=0)              {               rem=n%10;                 reverse=reverse*10+rem;               n/=10;              }              Console.Write("Reversed Number: "+reverse);            }     }  
30th Aug 2021, 3:19 AM
Arun Jamson
Arun Jamson - avatar