I have written a program to input a decimal number and convert it to a bcd. I have been able to remove the bugs, but my program is returning a random value upon entering the input. For example, if I enter 3 as the input it gives me 0 4210704 instead of 011. Kindly help me out, I think there is some problem with regards to returning the value of a variable in the array. Below is my program:
Code:
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int *dectobin(int dig);
int main()
{
int n,dig,i=0;
int bcd[100]={0},b;
printf("Enter the number to be converted into BCD");
scanf("%d",&n);
while(n!=0)
{
dig=n%10;
n=n/10;
b=dectobin(dig);
bcd[i]=b;
i=i+1;
}
for(;i>=0;i--)
{
printf("%d\t",bcd[i]);
}
system("pause");
return 0;
}
int *dectobin(int dig)
{
int r,i=0,j=0;
int array[100]={0};
static int reversearray[100]={0};
while(dig!=0)
{
array[i]=dig%2;
dig=dig/2;
i=i+1;
}
for(;i>=0;i--)
{
reversearray[j]=array[i];
j+=1;
}
return reversearray;
}