aspose file tools*
The moose likes Beginning Java and the fly likes inverse of a number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inverse of a number" Watch "inverse of a number" New topic
Author

inverse of a number

Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
Posts: 60
i want the code of a program that accept a number and display the inverse of it.
looking forward for your reply.
with respect,

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

****************************<br />For Some Dreams.....<br />One Lifetime Is Not Enough.<br />****************************
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39534
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
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.
please accept my apologies.
thank you.
Yelamuri Chandu
Greenhorn

Joined: Jan 08, 2008
Posts: 15

[ January 09, 2008: Message edited by: Fred Rosenberger ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10906
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10906
    
  12

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

Joined: Dec 29, 2007
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.
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

Hi friend try this:-

[ January 10, 2008: Message edited by: Fred Rosenberger ]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
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


You down with OOP? Yeah you know me!
Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Jan 24, 2006
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

Joined: Dec 29, 2007
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inverse of a number
 
Similar Threads
India Australia cricket
isElIgnored doubt from HFSJ pgno.336
Do doctors follow ethics...
A
what will happen if i do not write the main function in my program???