hi guys
i am trying to make a program to convert decimal number into binary .. i am able to do that, but the number comes inverted.. like 1101 comes like 1011
now how do i swap it off..
here is teh code -
import java.io.DataInputStream;
-
class dec2b{
-
-
public static void main(String a[]){
-
-
DataInputStream inp=new DataInputStream(System.in);
-
int nNum=0,nTemp=0,nSum=0,nBin=0,i=0;
-
-
-
System.out.println("Enter a Number to Convert into Binary");
-
-
try{nNum=Integer.parseInt(inp.readLine());}
-
catch(Exception e){}
-
-
while(nNum!=0)
-
{
-
-
nTemp=nNum;
-
-
if(nTemp%2==1)
-
nBin=1;
-
if(nTemp%2==0)
-
nBin=0;
-
-
nSum=nSum*10+nBin;
-
nNum=nNum/2;
-
-
}
-
System.out.println(nSum);
-
}}
-
please help me
thanks
14 26786
hi guys
i am trying to make a program to convert decimal number into binary .. i am able to do that, but the number comes inverted.. like 1101 comes like 1011
now how do i swap it off..
You don't; you should read the API documentation for the Integer class. It's all there.
kind regards,
Jos
You don't; you should read the API documentation for the Integer class. It's all there.
kind regards,
Jos
hi jos,
I know there is a straight function integer(11,2) .. but i wanna make it thru logic..
cause that is my assignment.. thanks :)
hi jos,
I know there is a straight function integer(11,2) .. but i wanna make it thru logic..
cause that is my assignment.. thanks :)
Recursion is your friend then (it avoids reversing the entire thing at the end).
Suppose you have a number 'n' less than 2: the result will be "0" or "1" then; otherwise
the result will be the binary string representation of the number n/2 appended with
the binary string representation of n%2. You can even avoid the division and
modulo by a bit of bit shifting (hint: >>> and <<).
kind regards,
Jos
Recursion is your friend then (it avoids reversing the entire thing at the end).
Suppose you have a number 'n' less than 2: the result will be "0" or "1" then; otherwise
the result will be the binary string representation of the number n/2 appended with
the binary string representation of n%2. You can even avoid the division and
modulo by a bit of bit shifting (hint: >>> and <<).
kind regards,
Jos
thanks seems interesting.. will try :)
thanks thanks a lot
hi jos,
I know there is a straight function integer(11,2) .. but i wanna make it thru logic..
cause that is my assignment.. thanks :)
Use BufferedReader or Scanner instead of DataInputStream for reading input.
To reverse an integer's digits, just make it a String, pass it to a StringBuilder and call the reverse method on it.
Recursion is your friend then (it avoids reversing the entire thing at the end).
Suppose you have a number 'n' less than 2: the result will be "0" or "1" then; otherwise
the result will be the binary string representation of the number n/2 appended with
the binary string representation of n%2. You can even avoid the division and
modulo by a bit of bit shifting (hint: >>> and <<).
kind regards,
Jos
There you go again ...
hello..can you tell me how to loop the decimal to binary conversion?
hello..can you tell me how to loop the decimal to binary conversion?
What do you mean by "how to loop"? For all the rest: read this thread.
kind regards,
Jos
- /*
-
* To change this template, choose Tools | Templates
-
* and open the template in the editor.
-
*/
-
-
package decimalbinary;
-
import javax.swing.*;
-
/**
-
*
-
* @author Admin
-
*/
-
public class Main {
-
-
/**
-
* @param args the command line arguments
-
*/
-
public static void main(String[] args) {
-
// TODO code application logic here
-
-
int x,i,q,z,temp=0;
-
q=0; z=0;
-
String a="";
-
x =Integer.parseInt(JOptionPane.showInputDialog("Please enter your number"));
-
-
while (x!=0)
-
{temp=x;
-
x=temp/2;
-
q=temp%2;
-
a=q+a;
-
-
}
-
String msg="Output is "+a;
-
JOptionPane.showMessageDialog(null, msg);
-
}
-
-
}
@jesz143
Accumulating the digits in a StringBuilder and then reversing it at the end (as suggested by r\d*x) would remove the need for all these string concatenations.
Is there a reason why this thread was woken from its well deserved rest?
- class Binary
-
{
-
public static void main(String args[])
-
{
-
int result=0;
-
int val=48,nbin=0,k=0;
-
while(val!=0)
-
{
-
-
nbin=(val%2==0)?0:1;
-
result=nbin*(int)Math.pow(10,k)+result;
-
val/=2;
-
++k;
-
-
}
-
System.out.println(result);
-
}
-
}
-
I hope this may work for you
@JosAH
Hello JosAH,
I posted my solution for decimal to binary conversion.Since am newbie I need a suggestion to modify my solution
pbrockway2: I like your solution. No need for recursion!
Modifying the original code a little: -
StringBuffer sb = new StringBuffer();
-
while(nNum!=0) // code changes nNum
-
{
-
if(nNum%2==1)
-
sb.append(1);
-
else // if(nNum%2==0)
-
sb.append(0);
-
nNum >>= 1; // same as nNum /= 2;
-
}
-
sb.reverse();
-
This is the Program Which will convert a Decimal no to Binary fie .. -
-
class Binary{
-
public static void main(String[] args){
-
int no=150; //Input the No here
-
int i=0;
-
int size=no/2; //the no of 0 and 1 is no/2 approximetly;
-
int a[]=new int[++size]; //creating an array of that size
-
while(no>0){
-
-
a[i++]=(no%2==0)?0:1;
-
no /=2;
-
}
-
-
for(int j=i-1;j>=0;j--){
-
-
System.out.print(a[j]);
-
-
}
-
-
}
-
}
-
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Raja |
last post by:
IS there any inbuilt decimal to binary conversion function in C++
|
by: nyy |
last post by:
Hello everybody on this group. I have this program that is supposed to
display a temp. conversion from fahrenheit to celsius, can anybody
kindly can tell me what is wrong? Thanks.
<!DOCTYPE html...
|
by: Mars |
last post by:
I'm writing a program for listing all binary numbers of the same length
with the same number of 1s.
e.g.
0011
0101
0110
1001
1010
1100
|
by: allanarendra_reddy |
last post by:
hi plz send me the logic for the binary convertio program
|
by: AsheeG87 |
last post by:
Hey Everyone~
I'm still a C++ Rookie so please bear with me on this.
I'm doing a temperature conversion program with prototype functions.
Basicly, I was wondering if some of you would take a look...
|
by: farahomer |
last post by:
Hi !!
I have a question. I am developing a code in C#.NET. So here I have a long string in this format: 000000000010 000000100100 (for example). Its 24 digit long, split into 12 digits by a space in...
|
by: dondigitech |
last post by:
I want to convert hex to binary without losing bits. I want to
preserve the 8-bits because I ultimately need a 24-bit string to grab
information from. I am just using this line of code for the...
|
by: star01daisy |
last post by:
This is what the assignment says to do: Write a C++ program to do decimal-binary number conversions. The program gives the user a choice of conversion type (binary to decimal or decimal to binary)....
|
by: Don Hillgen |
last post by:
I need to expand a field in a db2 table, must I write a conversion program???
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |