Decimal value for binary conversion in C

My simple program cannot convert 7 to 111 (current code gives 101). I know the myArray[]

latter printf()

can be improved, but we can talk about it 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;
}

      

+3


source to share


3 answers


This works great for me. do you have the header files right? and which compiler are 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 am also trying this. you can also try this if you like :)

#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


source


There are some bugs to take care of.



  • Initialize array because for conversion 1 it might be garbage 0 1
  • Do not press myArray[i] = quo;

  • You should take care of printing as i

    different lengths as possible and may lead to unstable behavior.
  • Also initialize quo

    before use, as this can lead to garbage comparisons.
  • Also follow the advice in the comments, they are helpful.

    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


source


From your logic that you used, you need to make small 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 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

0


source







All Articles