Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 25-11-2009
Member
 
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");
}
Reply With Quote
  #3  
Old 25-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
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; 
}
}
Reply With Quote
  #4  
Old 25-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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();
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "c program to convert decimal to binary"
Thread Thread Starter Forum Replies Last Post
How to convert a decimal number into words in Excel using VBA? Gandhik Windows Software 3 27-11-2010 12:59 AM
Convert float to 2 decimal place SoftWore Software Development 3 26-11-2009 01:22 PM
Convert binary to decimal in java Seraphim Software Development 2 20-05-2009 09:19 AM
Convert a string in decimal FlayoFish Software Development 3 23-04-2009 12:18 PM
Program to Convert Hexadecimal No into Binary No in C language Joyjeet Software Development 4 06-03-2009 01:12 PM


All times are GMT +5.5. The time now is 11:05 AM.