Thread: Unable to print the output of a conversion problem

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

    Unable to print the output of a conversion problem

    Dear All,

    I have written a program that accepts a decimal number as input and converts it to BCD representation. My code is unable to print the value of the output, feel free to point out the errors. Kindly find the code attached below:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
        int n,i=0,j=0,rem,bcd[100][100]={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");
        for(;i>=0;i--)
        {
           for(;j>=0;j--)
           {
                         printf("%d",bcd[i][j]);
           }
        }   
        system("pause");
        return 0;
    }
    Thank you for your help.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Why is bcd 100x100 if you are only using 100x4? Just declare it bcd[100][4].

    You need to initialize j in your for loop, otherwise, it's stuck at 0 after the first time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not able to print the output of the conversion problem
    By abhishekcoder in forum C Programming
    Replies: 11
    Last Post: 04-13-2012, 09:41 AM
  2. unable to print
    By nehal in forum C Programming
    Replies: 2
    Last Post: 03-09-2011, 08:01 AM
  3. Program unable to print anything
    By SasDutta in forum C Programming
    Replies: 7
    Last Post: 07-23-2010, 10:04 AM
  4. print output problem
    By chor in forum C Programming
    Replies: 1
    Last Post: 12-29-2002, 09:45 AM
  5. Link list problem->can't print the output...
    By frankiepoon in forum C Programming
    Replies: 1
    Last Post: 11-09-2002, 12:01 AM

Tags for this Thread