Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

a c program to convert decimal to binary

+2 votes
asked Jul 18, 2018 by anonymous

5 Answers

0 votes
answered Jul 20, 2018 by anonymous
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
long a[500],i,j=0,c;
float m=0;
int n,b[30];
char s;
clrscr();

up:cout<<"Enter number"<<endl;
scanf("%d.%f",&n,&m);

for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
j++;

}
i=0;
while(m>1)
{m=m/10;
cout<<"m="<<m<<endl;
i++;

}
cout<<endl<<"i="<<i<<endl;
c=i;

for(i=i;i>=1;i--)
{m=m*2.0;
  if(m>1)
{b[i]=1;
m=m-1;
}
else
{
b[i]=0;
}
cout<<"m="<<m<<endl<<"b["<<i<<"]="<<b[i]<<endl;
}

cout<<"Binary equivalent is:"<<endl;
while(j>0)
{
cout<<a[j-1];
j--;
}
 cout<<".";
for(i=c;i>=1;i--)
{ cout<<b[i];
}

cout<<endl<<"Do you want to convert next number?"<<endl<<"If yes press y if not press n"<<endl;
cin>>s;
if(s=='y'||s=='Y')
{goto up;
}
else
{
cout<<"Thank You!";
}

getch();
}
0 votes
answered Jul 22, 2018 by anonymous
#include<stdio.h>

#include<stdlib.h>

int main(int argc, char *argv[])

{

int x,r,i=0,j,q,s[10];

if(argc<2)

{

printf("please enter a number greater than zero\n");

exit(1);

}

x=atoi(argv[1]);

while(x>0)

{

s[i]=x%2;

x=x/2;

i++;

}

for(j=i-1;j>=0;j--)

{

printf("%d",s[j]);

}

}
0 votes
answered Jul 27, 2018 by flore
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int calcul( int val)
{
    int tmp;    //temporary binary number
    long int res;
    int start; //number used in the loop
    int power; //2*2*2 etc
    
    start = val;
    res = 0; //resultat binaire
    
    while(1)
    {
        tmp = 1;
        power = 1;
        while( (power * 2) <= start )
        {
            power = power * 2; //memorization of the value
            tmp *= 10; //we move the temporary number on the left
        }
        //add the new value to the old one
        res += tmp;
        
        //new number to determine
        start -= power;
        
        if( !start )
        {
            //that's the end
            break;
        }
        
    }
    
    return res;
}

int main()
{
    int val;
    int res;
    
    while (1)
    {
        printf( "enter the numer to convert or 0 to stop : ");
        scanf( "%d", &val );
        if( !val )
        {
            return 0;
        }
        //binary calculation
        res = calcul( val);
        printf( "binary (%d)\n", res );
        
    }

    return 0;
}
0 votes
answered Aug 3, 2018 by Raju G (370 points)
#include<stdio.h>

int main()

{

int a,nu,b[100];

scanf("%d",&n);

while(n!=0)

a[i]=n%2;

n=n/2;

i++;

while(i--)

printf("%d",a[i]);

}
0 votes
answered Aug 4, 2018 by dllanthous (140 points)
#include <stdio.h>

#include <math.h>

int main()

{

int x, teste;

scanf("%d", &x);

teste = pow(2,lenN(x));

while(teste>0)

{

if(x & teste)

{

printf("1");

}

else

{

printf("0");

}

teste=teste/2;

}

return 0;

}

int lenN(int x)

{

int cont=0;

while(x!=1)

{

x=x/2;

cont++;

}

return cont;

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...