Decimal to binary in C and C++

Bismillahir Rahmanir Rahim
Solution in C:

#include<stdio.h>
void main()
{
int a,b[50],i=0,c=-1,d;
printf("Enter your decimal number: ");
scanf("%d",&a);
while(a!=0)
{
 b[i]=a%2;
 a=a/2;
 i++;
 c++;
}
printf("Binary is =");
for(i=c;i>=0;i--)
 printf("%d",b[i]);
}
Solution in C++:

#include<iostream.h>
void main()
{
 int a,b[50],i=0,c=-1,d;
  cout<<"Enter your decimal number: ";
  cin>>a;
 while(a!=0)
 {
   b[i]=a%2;
   a=a/2;
   i++;
   c++;
 }
 cout<<"Binary is =";
 for(i=c;i>=0;i--)
  cout<<b[i];
}

Socializer Widget By Blogger Yard
SOCIALIZE IT →
FOLLOW US →
SHARE IT →