473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to Print Binary of a Decimal Number

Any one Write this Program in a Better Way ..

Expand|Select|Wrap|Line Numbers
  1. /*******
  2. *@Author Lokanath Behera
  3. *@Mob-[removed]
  4. *@Email-[removed]@gmail.com
  5. *****/
  6.  
  7. class Binary{
  8.     public static void main(String[] args){
  9.         int no=19999;
  10.         int i=0;
  11.         int size=no/2;
  12.         int a[]=new int[++size];
  13.         while(no>0){
  14.  
  15.         if(no%2==0){
  16.             a[i++]=0;
  17.         }
  18.         else{
  19.             a[i++]=1;
  20.  
  21.         }
  22.         no /=2;
  23.         }
  24.  
  25.         for(int j=i-1;j>=0;j--){
  26.  
  27.             System.out.print(a[j]);
  28.  
  29.         }
  30.     }
  31. }
  32.  
May 5 '13 #1
4 2418
Nepomuk
3,112 Expert 2GB
Define better. For example, an elegant way would be to do it recursively.
Even keeping with the loop approach, the length of the array can be calculated more accurately with
Expand|Select|Wrap|Line Numbers
  1. // The length will be the logarithm of no to base 2, plus 1
  2. int size = Math.log(no) / Math.log(2) + 1;
There are also ways to calculate logarithms to the base 2 more efficiently, so you could use those if efficiency is relevant.
Does that cover it or is there a specific way in which you want it improved?
May 6 '13 #2
vijay6
158 100+
Hey lokanath60, there is no need of if-else statement inside the while loop.
May 6 '13 #3
Nepomuk
3,112 Expert 2GB
Good point, you could just use
Expand|Select|Wrap|Line Numbers
  1. while(no > 0){
  2.   a[i++] = no % 2;
  3.   no /=2;
  4. }
May 6 '13 #4
Sherin
77 64KB
Try This Code

Expand|Select|Wrap|Line Numbers
  1. public class BinaryDecimal {
  2.  
  3.     public static void main(String[] args) {
  4.         long num = 110110111;
  5.         int decimal = convertBinaryToDecimal(num);
  6.         System.out.printf("%d in binary = %d in decimal", num, decimal);
  7.     }
  8.  
  9.     public static int convertBinaryToDecimal(long num)
  10.     {
  11.         int decimalNumber = 0, i = 0;
  12.         long remainder;
  13.         while (num != 0)
  14.         {
  15.             remainder = num % 10;
  16.             num /= 10;
  17.             decimalNumber += remainder * Math.pow(2, i);
  18.             ++i;
  19.         }
  20.         return decimalNumber;
  21.     }
  22. }
Jan 30 '21 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

26
by: Carramba | last post by:
Hi! How can I output value of char or int in binary form with printf(); ? thanx in advance
5
by: dmitrey | last post by:
hi all, could you inform how to print binary number? I.e. something like print '%b' % my_number it would be nice would it print exactly 8 binary digits (0-1, with possible start from 0) ...
0
by: castironpi | last post by:
On May 7, 3:31 pm, Mensanator <mensana...@aol.comwrote: ) for a in range( 10 ) ] ) 00000000 00000001 00000010 00000011 00000100 00000101 00000110
36
by: Kapteyn's Star | last post by:
hi group, i try to compile code below but my compiler is failing. it tells:- printbin.c: In function ‘main’: printbin.c:9: error: invalid operands to binary & i am not able to understand what...
0
by: Terry Reedy | last post by:
A. Joseph wrote: These are number representation systems that can be applied to or used with integral, rational (numberator,denominator), and 'point' numbers. Try Wikipedia or any search...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
Oralloy
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.