3

My simple program fails to convert 7 to 111 (the current code gives 101). I know myArray[] and the last printf() can be improved, but we can talk about that next time.

int main() {
    int myDecimal, quo, rem;
    int i = 0; //counter
    int myArray[3];
    printf("Enter valid decimal number: ");
    scanf("%d", &myDecimal);

    while(quo != 1){
        quo = myDecimal / 2;
        rem =  myDecimal % 2;

        myArray[i] = rem;
        myDecimal = quo;
        i++;
    } myArray[i] = quo;

    printf("\nBinary: %d %d %d", myArray[i + 2], myArray[i + 1], myArray[i] );  
    return 0;
}
16
  • 7
    Unrelated note, quo should be initialized to some value on the off chance the garbage value in memory Is 1. I'm currently looking at the rest of the code...
    – starlight
    Commented Apr 15, 2017 at 7:48
  • 2
    At the end of the loop, what is the value of i? Commented Apr 15, 2017 at 7:51
  • Do you include stdio.h? When I run the code it works just fine.
    – starlight
    Commented Apr 15, 2017 at 7:52
  • 1
    @ReiAllenPhillipRamos Yes, and so would not then i + 1 (and i + 2) be out of bounds? Commented Apr 15, 2017 at 7:54
  • 1
    Lastly a note unrelated to your problem, but don't use leading newline when printing with printf. The stdout file stream (which is what printf writes to) is by default line buffered. That means the output will actually be written when there is a newline in the string. If you have a leading newline you print the previous output, while the current output from the current printf call will not be printed. Make it a habit to always have a trailing newline in your printf calls. Commented Apr 15, 2017 at 7:58

3 Answers 3

0

It's work fine for me. have you include header files properly? and which compiler do you using ?

#include <stdio.h>
#include <conio.h>

int main() {
    int myDecimal, quo, rem;
    int i = 0; //counter
    int myArray[3];
    printf("Enter valid decimal number: ");
    scanf("%d", &myDecimal);

    while(quo != 1){
        quo = myDecimal / 2;
        rem =  myDecimal % 2;

        myArray[i] = rem;
        myDecimal = quo;
        i++;
    } myArray[i] = quo;

    printf("\nBinary: %d %d %d", myArray[i + 2], myArray[i + 1], myArray[i] );
    return 0;
}

Output

enter image description here

I also try this. you also can try this if you want :)

#include <stdio.h>
#include <conio.h>

int main() {
    long myDecimal;
    long binary = 0, i = 1;
    int rem;
    printf("Enter valid decimal number: ");
    scanf("%d", &myDecimal);

    while(myDecimal != 0) {
        rem = myDecimal%2;
        myDecimal = myDecimal/2;
        binary= binary + (rem*i);
        i = i*10;
    }
    printf("Binary number is %ld",binary);
}
0
0

There's some errors that should be taken care of..

  1. Initialize the array because for conversion of 1, it maybe garbage 0 1
  2. Don't force the myArray[i] = quo;
  3. You should take care of printing as i maybe of different length and may cause unstable behavior.
  4. Also initialize quo before use as it may lead to garbage comparison.
  5. Also follow the tips in comments, they are useful.

    int main() {
    int myDecimal, quo, rem;
    int i = 0; //counter
    int myArray[3] = {0};
    printf("Enter valid decimal number: \n");
    scanf("%d", &myDecimal);
    quo = myDecimal;
    while(quo > 0){
        quo = myDecimal / 2;
        rem =  myDecimal % 2;
    
        myArray[i] = rem;
        myDecimal = quo;
        i++;
    } 
    printf("Binary: %d %d %d", myArray[2], myArray[1], myArray[0]);  
    return 0;
    }
    
0

By your logic you have used you need to make smaller changes to get this to work.

check.c

#include<stdio.h>

int main() {
int myDecimal, quo, rem;
int i = 0; //counter
int myArray[3];
printf("Enter valid decimal number: ");
scanf("%d", &myDecimal);
//check myDecimal whether it's above 0 after each iteration
while(myDecimal > 0){
    quo = myDecimal / 2;
    rem =  myDecimal % 2;

    myArray[i] = rem;
    myDecimal = quo; 
    i++;
} myArray[i] = quo;
//print the array in the reverse order
for(i=2;i>=0;i--){
printf("%d",myArray[i]);
}  
printf("\n");
return 0;
}

Ouput: enter image description here

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.