0
votes

I am trying to make a c program that converts the decimal the user entered into a binary and octal number.

Given the user entered 24, my output should look like this:

24 in Decimal is 11000 in Binary.
24 in Decimal is 30 in Octal.

But the terminal only executes the decimal to binary conversion. Hence my current output look like this:

24 in Decimal is 11000 in Binary.
0 in Decimal is 0 in Octal.

Here is the part containing the conversions. It is inside a switch-case statement.

        {
        case 'A': case 'a': 
        {
            int a[10], input, i;  //variables for binary and the user input
            int oct = 0, rem = 0, place = 1; //variables for octal
            printf("Enter a number in decimal: ");
            scanf("%d", &input);

//decimal to binary conversion            
            printf("\n%d in Decimal is ", input);
            for(i=0; input>0;i++)
                {
                    a[i]=input%2;
                    input=input/2;
                }
            for(i=i-1;i>=0;i--)    
            {printf("%d",a[i]);}

//decimal to octal conversion
            printf("\n%d in Decimal is ", input);
            while (input)
            {rem = input % 8;
            oct = oct + rem * place;
            input = input / 8;
            place = place * 10;}
            printf("%d in Octal.", oct);

        }
            break;

The octal conversion only executes when I remove the decimal to binary portion. But I want both of them to execute at the same time.

for(i=0; input>0;i++) The input value is 0 when that loop completes. So of course it doesn't have the user entered value anymore for the next loop. You should be able to find such issues yourself with basic debugging - run your program in a debugger and examine it as it runs.kaylum