WAP(Write a program) to convert a decimal no. to binary form ;)

public class dec_bin
{
public static void main(String args[])
{
int num = 46000000, rem, a;
String str = “”, str2 = “”;
System.out.println(“no in decimal form = “+num);
do
{
rem = num %2;
str = str + rem;
num = num/2;
}
while(num != 0);
a = str.length() – 1;
for(;a >= 0; a–)
{
str2 = str2 + (str.charAt(a));
}
System.out.println(“No. in binary form = “+str2);
}
}

Friends, this can be done by simple and single loop but when you will enter a long no. then the answer would be quite big because of which integer or long won’t be able to hold their value and the program will thus be incorrect.

Thats why i have taken the answer in string which can hold almost any amount of value any big amount of memory can be stored in a string type. Thus above program will always give you the correct answer. 😀 enjoy…if you have not understood then contact me at facebook https://www.facebook.com/rishabh.gagan.9… (y)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s