Thursday, September 20, 2012

program for DECIMAL TO BINARY CONVERSION


/*DECIMAL TO BINARY CONVERSION*/
WITHOUT USING FUNCTION

 C program to convert decimal to binary: c language code to convert an integer from decimal number system(base-10) to binary number system(base-2)

#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[30],r,i=0,j;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
printf("the binary number requred is\n");
while(n)
{
a[i]=n%2;
n/=2;
i++;
}
j=i-1;
for(i=j;i>0;i--)
{
printf("%d",a[i]);
}
getch();
}




WITH FUNCTION(recursive)
recursive function is a function which call itself


#include<conio.h>
#include<stdio.h>
void gbit(int num);
void main()
{
int num;
clrscr();
printf("Enter the number\n");
scanf("%d",&num);
printf("\n\n");
gbit(num);
getch();
}
void gbit(int num)
{
int temp;
if(num)
{
temp=num%2;
gbit(num/=2);
printf("%d",temp);

}
}

No comments:

Post a Comment