Error in a decimal to bcd conversion problem

This is a discussion on Error in a decimal to bcd conversion problem within the C Programming forums, part of the General Programming Boards category; I have written a program to input a decimal number and convert it to a bcd. I have been able ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Error in a decimal to bcd conversion problem

    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;
    }

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    110
    Doesn't your compiler display any warning at this line?
    Code:
    b=dectobin(dig);
    It's totally wrong because you are assigning pointer to value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: Decimal to BCD Conversion
    By abhishekcoder in forum C Programming
    Replies: 12
    Last Post: 04-08-2012, 12:54 PM
  2. Binary to decimal conversion
    By caysonmars in forum C Programming
    Replies: 9
    Last Post: 06-17-2011, 10:24 PM
  3. Decimal to Hex Conversion
    By millsy2000 in forum C Programming
    Replies: 7
    Last Post: 08-10-2010, 01:04 PM
  4. Decimal to Binary conversion help
    By tru.cutru in forum C Programming
    Replies: 3
    Last Post: 07-08-2008, 10:17 PM
  5. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 08:51 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21