how to convert decimal number to binary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saima
    New Member
    • Oct 2006
    • 1

    how to convert decimal number to binary

    a program that takes a decimal number input from the user and displays its binary number equivalent in 12 bits on the screen.

    For example:
    Enter a decimal number: 32
    Its Binary Equivalent is : 000000100000
  • rajesh6695
    New Member
    • Oct 2006
    • 96

    #2
    Originally posted by saima
    a program that takes a decimal number input from the user and displays its binary number equivalent in 12 bits on the screen.

    For example:
    Enter a decimal number: 32
    Its Binary Equivalent is : 000000100000

    Do all process through recursive function....and print the values in the main function....

    Comment

    • vninja
      New Member
      • Oct 2006
      • 40

      #3
      uhhhh stay away from recursive programming!!!! ! yea its easy but it eats up mem and cpu usage!!!!!
      i.e.
      go to a porn site (and turn your popup blocker off!!!).
      this is what recursive programing does if each window was a recursive function call.

      pretty much each time you call a function (lets say b) the calling function (lets say a) is put on hold.
      therefor function a calls b and both are open until b closes/ends so that function a may continue. with recursion a calls a so a is put on hold while another instance of a is running. do this enough times and your comp will crash (well might as well).

      i'd suggest a loop for this. something along the lines of

      removed as per posting guidlines


      the a%2 gives the remainder of number divided by 2.
      num=b/2 decreases the number (moves over 1 bit) until num = 1 and the loop ends. this SHOULD give you a bin representation of the number. however its not going to be with 12 bits. try n figure out the rest if need furthur help post it up! gl

      Comment

      • cplusplusmasta1337
        New Member
        • May 2007
        • 3

        #4
        removed as per posting guidlines


        BOOYAH!!!!!1111 11 I AM TEH_MASTA1!11

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          Originally posted by cplusplusmasta1 337
          removed as per posting guidlines


          BOOYAH!!!!!1111 11 I AM TEH_MASTA1!11
          Who apperently can't spell. ;) :D


          Adrian

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Originally posted by vninja
            uhhhh stay away from recursive programming!!!! ! yea its easy but it eats up mem and cpu usage!!!!!
            There are some problems that are just easier, conceptually and memory-wise, to solve with recursion. One example is traversing a binary tree - it's possible using loops, but I don't want to deal with all of that.

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by Ganon11
              There are some problems that are just easier, conceptually and memory-wise, to solve with recursion. One example is traversing a binary tree - it's possible using loops, but I don't want to deal with all of that.
              Further to what Ganon is saying, compiler optimisers nowadays can do some unrolling of recursion functions.


              Adrian

              Comment

              • cplusplusmasta1337
                New Member
                • May 2007
                • 3

                #8
                Again, code removed as per posting guidelines


                BOOYA!!! 1337 PROGRAM RIGHT HERE. I spell fine.

                Adrian.
                Last edited by Ganon11; May 18 '07, 12:44 PM. Reason: coded solution removed

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Originally posted by cplusplusmasta1 337
                  Again, code removed as per posting guidelines


                  BOOYA!!! 1337 PROGRAM RIGHT HERE. I spell fine.

                  Adrian.
                  ROFLMAO L7


                  Adrian

                  Comment

                  • cplusplusmasta1337
                    New Member
                    • May 2007
                    • 3

                    #10
                    removed as per posting guidelines


                    Soak it in boys. Lookin's for free, touchin's gonna cost ya.
                    Copy & Paste this for TEH_1337 HAX!!!!

                    Adrian. are you drunk?
                    // Nope but this oughta do it.

                    Comment

                    • AdrianH
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1251

                      #11
                      Originally posted by cplusplusmasta1 337
                      removed as per posting guidelines


                      Soak it in boys. Lookin's for free, touchin's gonna cost ya.
                      Copy & Paste this for TEH_1337 HAX!!!!

                      Adrian. are you drunk?
                      // Nope but this oughta do it.
                      Whatever, you are banned for a week. Do it again and you will be banned permanently.


                      Adrian

                      Comment

                      • Silent1Mezzo
                        New Member
                        • Feb 2007
                        • 208

                        #12
                        Sigh, stupid people....They' ll never learn.

                        Comment

                        • Savage
                          Recognized Expert Top Contributor
                          • Feb 2007
                          • 1759

                          #13
                          Originally posted by Silent1Mezzo
                          Sigh, stupid people....They' ll never learn.
                          Don't be rude and don't be a Savage

                          :)


                          Savage

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            If the input number is always going to contain the same number of bits, then you could just do an & operation for each bit (or iterate through the bits)
                            Code:
                            if (mydecimal&0x0004==0x0004)
                            {
                            //print a 1
                            }
                            else
                            {
                            //print a 0
                            }
                            
                            if (mydecimal&0x0002==0x0002)
                            {
                            //print a 1
                            }
                            else
                            {
                            //print a 0
                            }
                            if (mydecimal&0x0001==0x0001)
                            {
                            //print a 1
                            }
                            else
                            {
                            //print a 0
                            }

                            Comment

                            • AdrianH
                              Recognized Expert Top Contributor
                              • Feb 2007
                              • 1251

                              #15
                              Originally posted by Plater
                              If the input number is always going to contain the same number of bits, then you could just do an & operation for each bit (or iterate through the bits)
                              Code:
                              if (mydecimal&0x0004==0x0004)
                              {
                              //print a 1
                              }
                              else
                              {
                              //print a 0
                              }
                              
                              if (mydecimal&0x0002==0x0002)
                              {
                              //print a 1
                              }
                              else
                              {
                              //print a 0
                              }
                              if (mydecimal&0x0001==0x0001)
                              {
                              //print a 1
                              }
                              else
                              {
                              //print a 0
                              }
                              Two problems with this. 1. prints out the digits backwards. 2. used copy coding methods which are prone to error.

                              Other than that, it great. ;)


                              Adrian

                              Comment

                              Working...