Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
2.00/5 (6 votes)
See more:
Hello
i want code for reverse number in c# from number A to number B
for Example : 101 ~ 105 (101-102-103-104-105) ( a = 101 , b = 105 )
reverse number: 101 , 201 , 301 , 401 ,501

and i know this code only for reverse one number
C#
int a = Convert.ToInt16(txt1.Text);
while (a != 0)
{
   r = r * 10 + (a % 10),r=0;
   a = a / 10;
}



now what can i do ?
Regards.

--------------------------
i want complete code ! i think i need to use from "for" but i don't know how :D i test this
C#
int a = Convert.ToInt16(txt1.Text), b = Convert.ToInt16(txt2.Text);
            int r = 0;

            for (int i = a; i <= b; i++)
            {
                while (a != 0)
                {
                    r = r * 10 + (a % 10);
                    a = a / 10;
                }
                lblshow.Text += r + " , ";
            }

but this is not work ...!!! :(

for Example :
a = 101 in textbox1
and
b = 105 in textbox2

and i want show in lbl
101 , 201 , 301 , 401 , 501
Posted
Updated 22-Aug-12 3:48am
v6
Comments
AshishChaudha 22-Aug-12 8:45am    
you know the code then what your problem is??
ridoy 22-Aug-12 8:46am    
you need to apply your logic..if we give solution to you then your logic willn't be developed.
bbirajdar 22-Aug-12 8:46am    
Hint: Add this while loop inside another for loop...
I.explore.code 22-Aug-12 8:57am    
you almost have the solution, all u need to do simple string manipulation and concatenation and that's it!
mehrdad1991h 22-Aug-12 9:38am    
I don't want use from string !
I have solution, But this is not complete / correct i know i must use from while an for both but i can't ! i use but that is not work !

What you have to do is split string for some character, in your case, its '-' and save it in array and for array count, pass each element in your reverse method..

For eg.

C#
string s = "101-102-103-104-105";
   
    string[] numbers= s.Split('-');
    foreach (string num in numbers )
    {
       int a = Convert.ToInt16(num);
        int result=0;
     do
     {
         result = (result * 10) + (a% 10);
         a= a/ 10;
     }while (a> 0);
    
    Console.WriteLine(result);
    }
 
Share this answer
 
v2
Comments
I.explore.code 22-Aug-12 10:35am    
I would take this as a solution! And just use a little bit of my own work to make it sing to my tune in no time! My 5!
As it is a logical problem i refer you to do it by yourself.you can solve it as you can do it for one number.i give you one trick,apply this one number logic to every number of a 3 digit number.only you need to customize it when it is 0 otherwise for every case it is same.You can use loop to do it.
Ok,if you fail in your try then see it..
http://wiki.answers.com/Q/Write_a_program_in_c_language_to_reverse_a_three_digit_integer_number_without_using_loops[^]
 
Share this answer
 
v2
Comments
Vani Kulkarni 22-Aug-12 9:03am    
Good link! My 5!
ridoy 22-Aug-12 12:08pm    
thanks Vani
I found solution :D and i don't use from string method :D
I want This code....

C#
{
    int a = Convert.ToInt16(txt1.Text), b = Convert.ToInt16(txt2.Text);
    int r = 0,s=0;
    lblshow.Text = null;

    for (int i = a; i <= b; i++)
    {
        s = i;
        while (s > 10)
        {
            r = r * 10 + (s % 10);
            s = s / 10;
        }
        r = r * 10 + (s % 10);
        lblshow.Text += r + " , ";
        r = 0;
    }
}
 
Share this answer
 
Comments
I.explore.code 22-Aug-12 11:34am    
was that so complicated? :)
mehrdad1991h 22-Aug-12 11:39am    
:D Umm , Yeah ! ((for me !!!))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900