Buy Questpond's video subscriptions on
huge discount
.
Online: 3815
Home
Articles
Interviews
Forums
For Beginners
Popular Questions
ITIL Career Advice
PMP Career Advice
Career Advices
Codes
Videos
ASP.NET
ASP.NET MVC
Android Intel XDK
Sql Server
AngularJS
Bootstrap
Backbone.JS
MongoDB
LESS (CSS)
jQuery
WPF
WWF
SSIS
LightSwitch
Tutorials
News
ASP.NET MVC
|
Be Interview Ready
|
Top Performers
|
DNF MVP
|
Top Posts
|
Winners
|
Subscribe
|
Catalogs
Welcome Guest !
Register
Login
Home
>
Forums
>
C#
>
Loading ...
how to reverse an integer starting or ending with 0
Posted by
Tanwar18
under
C#
on 3/20/2012 |
Points: 10
| Views : 9045 | Status :
[Member]
| Replies : 12
Write New Post
|
Search Forums
|
Resolved Posts
|
Un Answered Posts
|
Forums Home
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.
Reply
|
Reply with Attachment
Alert Moderator
Responses
Posted by:
Savariya
on: 3/20/2012
[Member]
Starter
|
Points: 25
0
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
0
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
0
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
0
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
0
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
0
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
0
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
0
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
0
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
0
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
0
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
0
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
Latest Posts
How to get the exact property name while getting error
(0)
PDB file is not showing Line Number in dot net application hosted on server
(0)
Both Strings morethan 5 letters in length end of the words one vowel and one consonent is different
(0)
how to check Any adjacent letters transposed between two strings(ex: JOHN, JHON)
(1)
can't receive data after success login ?
(1)
Implement Multi-Tenant in Azure Logic Apps
(0)
Why ASP.Net Core 7.0 Web API showing as Connection refused?
(0)
Iterating over columns of dataframe and print as rows in Python Django
(0)
More ...