Dear All,
I have written a program to convert the individual digits of the input numbers to bcd and print the value. My program is not displaying the output in the proper format, for example if i enter 12 as my input I am getting 000010000100 whereas I am expecting 0001 0010. Please help me out. Thank you.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int n,i=0,j=0,rem,bcd[100][4]={0},ndigit=0;
/*n is the input number, rem stores the individual digits of the input number for conversion bcd[100][100] is the array used for storing the converted values, with the first index storing the number of digits and the second the value, for example if 14 is given as an input number, bcd[0][0...3] stores 0100 and bcd[1][0...3] stores 0001*/
printf("Enter the number to be converted");
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
n=n/10;
/*this loop converts the individual digits of the input decimal into bcd*/
while(rem!=0)
{
bcd[i][j]=rem%2;
rem=rem/2;
j+=1;
}
i+=1;
j=0;
}
printf("The BCD representation of the input number is\t");
for(;i>=0;i--)
{
for(j=0;j<=3;j++)
{
printf("%d",bcd[i][j]);
}
}
system("pause");
return 0;
}