# inverse of a number

Ammar Salem
Ranch Hand
Posts: 60
i want the code of a program that accept a number and display the inverse of it.
with respect,

[ January 08, 2008: Message edited by: Ulf Dittmer ]
[ January 08, 2008: Message edited by: Bear Bibeault ]

Ulf Dittmer
Rancher
Posts: 42968
73
If you want the code for something then you'll just have to write it, won't you? What ideas for solving this have you had so far?

We have a page that collects some hints on how to get the most out of the Saloon: http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch, in particular ShowSomeEffort, NotACodeMill and EaseUp.

Ammar Salem
Ranch Hand
Posts: 60
Thank you for letting me know, I did not mean to cross the limit of asking a question.Anyway the program was my home assignment and i tried to do it. I just thought it is not necessary to write my code. I wanted it ready made !! how sad. from now on, I will undoubtly abide by the Ranch rules.
thank you.

Yelamuri Chandu
Greenhorn
Posts: 15

[ January 09, 2008: Message edited by: Fred Rosenberger ]

fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Yelamuri Chandu,

Please learn our policies. Three times, you have posted a solution. three times it has been removed. At the top of the main page of this forum, it states:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

DO NOT post a solution again.

fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Ammar,

We all here to help. As Ulf suggested, post what ideas you have here, what you've tried, etc. Give us a better idea where you are stuck.

and finally, give a better idea what you are trying to do. "inverse of a number could mean several things. there is an additive inverse, a multiplicative inverse, or just reversing the digits. a better defined problem is easier to solve.

Ammar Salem
Ranch Hand
Posts: 60
Fred Rosenberger,
Hi, I have never written any programs before in any language,I am totally new to java. I have tried to do the program but I really need the slightest hint on how to inverse the digit given to the program.I will post my code so that you can have a look.

I know it looks awful and the logic is not correct,but this is what I have been trying to do.I think I will be using two for loops, but as I told you, i still can't apply the proper logic of inversing the digits.anyway,I am looking forward for your suggestions.
thank you.

Ranch Hand
Posts: 180
Hi friend try this:-

[ January 10, 2008: Message edited by: Fred Rosenberger ]

Justin Fox
Ranch Hand
Posts: 802
If you are getting a string representation of an integer back from the user,
putting it in an array, and then printing it in reverse would be easier.

for example:

if you had an array with values ("1","2","3");

how would you go about printing that array in reverse?

Justin Fox

Ammar Salem
Ranch Hand
Posts: 60
Originally posted by Justin Fox:
If you are getting a string representation of an integer back from the user,
putting it in an array, and then printing it in reverse would be easier.

for example:

if you had an array with values ("1","2","3");

how would you go about printing that array in reverse?

Justin Fox

i really have no clue Justin? would you help me out.I am stuck.

Campbell Ritchie
Sheriff
Posts: 49367
62
What Justin means is that you get an input, which he has maintained as a String, convert it to a char[] array (go through the String class API) then print out the array backwards. Find the Java Tutorial and look for arrays to find out how to do this.

I am afraid that is not quite what you have been given. You are probably told to print the individual numbers from a decimal integer using /10 and %10 operators repeatedly to get the individual digits. If you get the operators right to find the last digit, you can print it out and repeat.

That is about as much detail as we dare give you.

Justin Fox
Ranch Hand
Posts: 802
This indeed is a very simple task, whether it be the way I was speaking of, or the /10 and %10 way.

if you go to www.sun.com and go under developers->api/docs
you'll definatly find what you need.

Java, in my opinion, has the best online API and documentation out of any other programming language.

I don't know what you're doing this for. If it's a homework assignment or what, but actually looking for the solution, or getting a book on java is the most help. I know it probably sounds good to just get someone to give you the answer, but when you actually figure it out yourself and it works! That's the best part of all...

If this type of problem is very difficult for you, I suggest you get a beginners JAVA book and sit and read it. The only way to get better at java is to practice practice practice... but if you try to start on the harder projects you'll never get anywhere if you don't build up to them.

But lets take a second to think about the process in which to approach a task like this. I'm going to assume that it's the string representation of a number approach and run with that...

ok, so you need to get input from the user right?
- well you did good by using the BufferedReader.
- you could also look into the java.util.Scanner class

now you need to be able to get that darn string into a character array...
- look into the string class and it's many different functions
- I'm sure you'll find what you need.

now you need to be able to get that character array in reverse!?
- again, you did good by using a for loop.
- usually you would start with i = 0, and then increment to go foward
- through the array, well use that knowledge and do the opposite.

if you were needing to figure out how to do this task mathmatically, as in
actually finding the last digit and stuff... You're probably just as lost as I am.
- but I'm sure if you look into the process of finding the reverse of a number numerically by google searching the topic, I'm sure you'll get what you need.

I hope you find what you are looking for and succeed in
getting this done.

Justin Fox

Ammar Salem
Ranch Hand
Posts: 60
Thank you both Justin fox and Campbell Ritchie you have been endlessly helpful.You have enlighten me just fine. now I will do as you both suggested.Actually, this program is my home assignment.My java knowledge is not bad but sometimes applying a certain logic to something is quite hard.The thing is that I have been thinking how can I write a function for reversing the digit given as an input.Now as i said I should work it out and if i succeed I will sure post my code here so that you can see it.
thank you again,
Ammar salem