I am confused "HOW to make a series of numbers in reverse orDer In java" I got the basic idea but i cannot join them together ...in this program compUtter Will identify weather input is binary or decimal .i am unable to make computer identify numbers in.please help wit Java coding.

Recommended Answers

All 9 Replies

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

Reverse the order of an array, perhaps?

Please show what you have tried already to identify when a number is received as a binary value or a decimal one. We can then give you refinements of your idea. Read your notes from class and your textbook, if you have one, to see if there are some ideas there on how this kind of thing can be done.

Public static .....(Int n)
{
Int c=0;
Double d=0,r ;
While(n!=0)
{
r=n%10;
d=d+r*Math.pow(2,c);
n=n/10;
c=c+1;
}System?out.println(+d);}
Today sir showed the simplest converting program to convert in decimal ...it is so simple i never imagine .thanks for support ..

Why are you using base 10 for your division and moulus operations, but base 2 for your power? You may want to change that 2 if you are not getting the results you expect.

If u want to change decimal to binary then instead of 2 in the math.pow Write 10.
If u want dry run then i can explain...i do have same doubt..but today there was no computer lecture So i would give the answer after 2 day's....tommorom is public holiday for me..i will try my best!.

I am confused myself too. It seems that you ask for one thing but your requirement asks for another???

HOW to make a series of numbers in reverse orDer In java

Doesn't it mean revert a number (i.e. 12345 --> 54321)?

Will identify weather input is binary or decimal .i am unable to make computer identify numbers

Well, what does binary number consist of? Of course, 0 and 1. The only problem you would have problem distinguishing a binary and decimal is that when a number consist of only 0 and 1. Why? Because it could also be a decimal. (i.e. 101 could be 101 in decimal or (4+0+1) in binary.)

Unless the number contains leading zeros (i.e. 0110 is binary), you will not know whether it is a binary or decimal number.

PS: You could convert decimal to binary and vice versa using Integer.toString() as well. However, I doubt this would be the purpose of your assignment.

I think this is your algorithm.
Assume you are tryning to convert Decimal 9 to binary value.
We know that (9) in base 10 = 1001 in base 2( i.e in binary).
So we use popular double dabble method for this conversion ( I also see this algorithm is used your posted code here).

So to convert decimal 9 to binary using double dabble method below is the entire process.

(divide 9 by 2=4) and produces-----remainder-->1
(divide 4 by 2=2) and produces-----remainder-->0
(divide 2 by 2=1) and produces-----remainder-->0
(divide 1 by 2=0) and produces-----remainder-->1 (you need to stop here because the result of division is zero)

   Read the reaminders from top to bottom--->1001 (result in binary)

Now to get the binary result read all the remainders from top to bottom. hence for the above example binary 9 is 1001
So use this alogoritm in your code to convert any number. Since you are reading from top to bottom you need the reverse function as stated in your requirement.
Below are the suggested modifications for your code .

a)What i see from your code is that you dont require a double in the code at line-4

Double d=0,r ;// not required.

b)Also the code at line number 7

r=n%10;//--> this should be n%2;

c)Then the code at line number 9

 n=n/10;//--> should be n=n/2;

d)Then initialize a string-buffer variable at the start of the program and append the value of r in the while loop(this acts as buffer where your are storing the remainders in each iteration);

e) after the while loop just reverse the string-buffer variable to get the desired binary value.

Please correct me If I am missing anything or I am incorrect in understanding the problem here

I can post the complete working code here. But that would be against the forum rules.

I am confused about this requirement

in this program compUtter Will identify weather input is binary or decimal

Because in your code the function is taking an "int" as the argument.
Can you please be clear about this

well it was my vacation assignment and i was told tat comp will identify....but when school reopned sir gave the program.....then i posted it tat it was solved....its obivious tat u will suspect me.....so i am sorry!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.