Output of conversion problem not displaying in the proper format

This is a discussion on Output of conversion problem not displaying in the proper format within the C Programming forums, part of the General Programming Boards category; Dear All, I have written a program to convert the individual digits of the input numbers to bcd and print ...

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

    Output of conversion problem not displaying in the proper format

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

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    639
    0001 0010 thats 18 in binary not 12

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    I want to convert my input decimal to BCD, 0001 0010 is bcd of 12.

    Quote Originally Posted by camel-man View Post
    0001 0010 thats 18 in binary not 12

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    639
    ahh ok did not see bcd there

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to print the output of a conversion problem
    By abhishekcoder in forum C Programming
    Replies: 1
    Last Post: 04-13-2012, 12:16 PM
  2. Not able to print the output of the conversion problem
    By abhishekcoder in forum C Programming
    Replies: 11
    Last Post: 04-13-2012, 10:41 AM
  3. Proper format/standard of C program?
    By zerokernel in forum C Programming
    Replies: 3
    Last Post: 11-17-2010, 01:15 AM
  4. can any one make this program run.. in proper output?
    By randyjhon143 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2009, 09:22 AM
  5. Displaying a calender in this format...
    By Claire in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2002, 04:24 AM

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