Results 1 to 4 of 4

Thread: c program to convert decimal to binary

  1. #1
    Join Date
    Nov 2009
    Posts
    65

    c program to convert decimal to binary

    I am studying inF.Y.B.Sc. I.T.and I got a assignment from college to create a program using C language that converts decimal number into binary number. I think it is very troublesome so suggest any function that convert decimal number into binary number. If there is no function then give me code.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: c program to convert decimal to binary

    code to convert decimal number to binary number

    Code:
    #include <stdio.h>
    
     void dec_bin(int num);
    
      int main(void) 
    {
       int m = 0;
    
        printf("Digit (0-255): ");
        scanf("%d", &m);
     
       (m >= 0) && (m < 256) ? dec_bin(m) : exit(1);
    
       return 0;
    }
    
      void dec_bin(int num) 
    {
     int x, y;
     x = y = 0;
    
     for(y = 7; y >= 0; y--)
     {
      x = num / (1 << y);
      num = num - x * (1 << y);
      printf("%d", x);
     }
    
     printf("\n");
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: c program to convert decimal to binary

    Code:
    #include <stdio.h>
    void dec2bin(long deci, char *bin);
    int main()
    {
        long dec;
        char bin[80];
        printf("\n\n Enter an integer value : ");
        scanf("%ld",&deci);
        dec2bin(deci,bin);
        printf("\n The binary value of %ld is %s \n",deci,bin);
        getchar(); 
        getchar();
        return 0;
    }
    
    void dec2bin(long deci, char *bin)
    {
       int k = 0, n = 0;
       int neg_flag = 0;
       int remain;
       int old_deci; 
       char temp[80];
    
    if (deci < 0)
    {
      deci = -deci;
      neg_flag = 1;
    }
    
    do
    {
       old_deci = deci; 
       remain = deci % 2;
       deci = deci / 2;
       printf("%d/2 = %d remainder = %d\n", old_deci, deci, remain);
       temp[k++] = remain + '0';
    } while (deci > 0);
    
     if (neg_flag)
        temp[k++] = '-'; 
    else
        temp[k++] = ' '; 
    
     while (k >= 0)
    {
        bin[n++] = temp[--k];
        bin[n-1] = 0; 
    }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: c program to convert decimal to binary

    Code:
    #include<stdio.h>
    #include<conio.h>
    void showbits(int m)
    {
           if(m==1)
           printf("%d",m);
          else
        {
          showbits(m/2);
          printf("%d",m%2);
        }
    }
    void main()
    {
           int n;
           void showbits(int m);
           clrscr();
           printf("Number?");
           scanf("%d",&n);
           printf("\nBin eq of %d is ",n);
           showbits(n);
           getch();
    }

Similar Threads

  1. Replies: 3
    Last Post: 27-11-2010, 12:59 AM
  2. Convert float to 2 decimal place
    By SoftWore in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 01:22 PM
  3. Convert binary to decimal in java
    By Seraphim in forum Software Development
    Replies: 2
    Last Post: 20-05-2009, 09:19 AM
  4. Convert a string in decimal
    By FlayoFish in forum Software Development
    Replies: 3
    Last Post: 23-04-2009, 12:18 PM
  5. Program to Convert Hexadecimal No into Binary No in C language
    By Joyjeet in forum Software Development
    Replies: 4
    Last Post: 06-03-2009, 01:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,998,892.66395 seconds with 17 queries