Answered # project help!, to convert binary numbers to decimal numbers and viceversa

JamesCherrill 2,785 Nutster 58 Discussion Starter meenal9 Nutster 58 Discussion Starter meenal9 Taywin 311 subramanya.vl subramanya.vl Discussion Starter meenal9

0

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.

*Edited 4 Years Ago by meenal9*

0

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.

0

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.

0

```
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 ..
```

0

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.

0

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!.

0

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.

*Edited 4 Years Ago by Taywin*

0

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.

*Edited 4 Years Ago by subramanya.vl*

0

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

*Edited 4 Years Ago by subramanya.vl*

0

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!!

This question has already been answered. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...