Thread: convert decimal to binary

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    30

    convert decimal to binary

    Code:
    #include<stdio.h>
    void main(){
     float a;
     int arra[100];
     int i=0,j;
     scanf("%f",&a);
     while(a>0){
      
      if((int)a%2!=0)
       arra[i]=1;
      else
       arra[i]=0;
      
      i++;
      a=a/2;
      if(a!=(int)a)
       a=a-0.5;
     } j=i;
     for(i=j;i=0;i--)
      printf("%d",arra[i]);
    }
    \
    nothing is printed.please help

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tommytmh View Post
    nothing is printed.please help
    Because of this:

    Code:
     for(i=j;i=0;i--)
    Few other things:

    - The return type of main() is int, not void.
    - There is no point at all in using a float here, since you cast to int when it counts anyway -- you even round down. Also, floating point numbers are represented differently. You might want to start here if you want to figure out the binary:

    Floating point - Wikipedia, the free encyclopedia

    You are doing the straightforward power of two unsigned int representation, so your input should be an unsigned int.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    simply u can keep on %2 the decimal number till it is >0 and store each remainder in an array..
    later on reverse print the array ...u get the binary easily...

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    23
    You could use itoa to obtain directly the number in binary as a char array.

    Check this tutorial out:
    Dystopian Code: Radix Conversion in C
    See here itoa reference:
    itoa - C++ Reference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert Decimal IP To Dotted Decimal Notation
    By MaSSaSLaYeR in forum C Programming
    Replies: 11
    Last Post: 11-16-2011, 06:18 PM
  2. Convert binary to decimal string
    By Devils Child in forum C# Programming
    Replies: 9
    Last Post: 01-26-2010, 07:37 AM
  3. how to convert decimal to hexa decimal in C/
    By kalamram in forum C Programming
    Replies: 4
    Last Post: 09-03-2007, 07:39 AM
  4. Convert decimal to binary
    By planet_abhi in forum C Programming
    Replies: 1
    Last Post: 12-20-2002, 04:47 AM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM