how to reverse an integer starting or ending with 0

Posted by Tanwar18 under C# on 3/20/2012 | Points: 10 | Views : 1935 | Status : [Member] | Replies : 12
how to reverse an integer starting or ending with 0 without using the reverse() method.
eg. 110 should be printed as 011. The simple program doesn't print 0 in the begining. plz help me out.




Responses

Posted by: Savariya on: 3/20/2012 [Member] Starter | Points: 25

Up
0
Down
public int reverse(int no)
{
int rem=0, ans=0;

while (no > 0)
{
rem = no % 10;
ans = ans * 10 + rem;
no = no / 10;
}
return ans;
}

Use this function its a simple reverse function

Thanks
Chintan

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tanwar18 on: 3/20/2012 [Member] Starter | Points: 25

Up
0
Down
but this will not print 110 into 011. it will print only 11.

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Savariya on: 3/20/2012 [Member] Starter | Points: 25

Up
0
Down
Updated answer

public string reverse(int no)
{
int rem = 0;
string fans = "";

while (no > 0)
{
rem = no % 10;

fans=fans+rem;
no = no / 10;
}
return fans;

}

Thanks
Chintan

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Veerac85 on: 3/20/2012 [Member] Starter | Points: 25

Up
0
Down
A more efficient way to achieve this is having .NET API

string str = "110";
char[] array = str.ToCharArray();
Array.Reverse(array);

Regards
Veera

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tanwar18 on: 3/21/2012 [Member] Starter | Points: 25

Up
0
Down
this is still not working Mr. Chintan.

We dont have to use any inbuilt method like reverse()..

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Savariya on: 3/21/2012 [Member] Starter | Points: 25

Up
0
Down
Hi tanwar
Can you tell me your code because this code is working fine at my end so............

Thanks

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tanwar18 on: 3/21/2012 [Member] Starter | Points: 25

Up
0
Down
static void Main()
{

Console.WriteLine("Enter any digit");
string ab = Console.ReadLine();
int sol = int.Parse(ab);
//int sol = 12340;
int rev = 0;
int r = 0;
while (sol > 0)
{
r = sol % 10;
rev = rev * 10 + r;
sol = sol / 10;

}
Console.Write(rev);
Console.ReadLine();

}

Hi Chintan, this program is working but when i gave no, like 120, it's printing only 21. 0 is missing.

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Savariya on: 3/21/2012 [Member] Starter | Points: 25

Up
0
Down
Hi tanwar

Do this modification at your code
static void Main()
{

Console.WriteLine("Enter any digit");
string ab = Console.ReadLine();
int sol = int.Parse(ab);
//int sol = 12340;

//Make this variable as a string
string rev = "";

int r = 0;
while (sol > 0)
{
r = sol % 10;
//I MODIFIED THIS
rev = rev + r;
sol = sol / 10;
}
Console.Write(rev);
Console.ReadLine();\
}

Thanks
Chintan

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tanwar18 on: 3/21/2012 [Member] Starter | Points: 25

Up
0
Down
thanks Mr. Chintan,it's working but if we input 011 then same problem happens,means 011 should be printed as 110 but o/p is only 11. 0 is again missing.

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Savariya on: 3/21/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Tanwar
Then you have to make the variable as a string and following is your code

static void Main()
{

Console.WriteLine("Enter any digit");
string ab = Console.ReadLine();
int len=ab.Length;
//Make this variable as a string
string rev = "";

len--;
while (len>=0 0)
{
rev=rev+ab[len];
len--;
}
Console.Write(rev);
Console.ReadLine();\
}

Thanks
Chintan

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tanwar18 on: 3/21/2012 [Member] Starter | Points: 25

Up
0
Down
thank you mr. Chintan, thanks a lot!.

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Savariya on: 3/21/2012 [Member] Starter | Points: 25

Up
0
Down
You welcome tanwar i hope u will got solution for your problem

Tanwar18, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response