1

I'm new to C and I'm trying to convert decimal to binary. The result is giving me an inverse number of what's required. I tried to apply modulo on the result like I saw on other forums but I still get the same result. Any help?

#include<stdio.h>

int main()
{
int number;
long int quotient, rem;
printf("Enter a number: ");
scanf("%d", &number);

quotient=number;

while (quotient!=0)
{
    quotient=quotient/2;
    rem=quotient%2;
    printf("%ld", rem%10);
    rem/=10;
}
}

I took an advice basing on a Print Function but still not sure if i understand it stills give me the same result. Please have a look.

#include<stdio.h>

void Print(int number,int base)
{
if (number >= base)
    Print(number/base,base);
printf("%d",number%base);
}

int main()
{
int number;
long int quotient, rem;
printf("Enter a number: ");
scanf("%d", &number);

quotient=number;

while (quotient!=0)
{
    quotient=quotient/2;
    rem = quotient%2;
    Print(rem, 2);
    }
}

Just a small note because i forgot to say and I don't want you to go through the trouble. The idea os to not use arrays. Thanks

user2985083
  • 63
  • 1
  • 8
  • 1
    See http://stackoverflow.com/a/112956/1274314 – TypeIA Jan 20 '14 at 17:42
  • well first of all i know how to reverse a given number and i could check the internet on how to convert decimal to binary but i didn't i did it by myself but i'm just getting stuck on the result i'm getting – user2985083 Jan 20 '14 at 17:46
  • ohh and i can't use arrays – user2985083 Jan 20 '14 at 17:46
  • Great! It's good that you're taking the time to write and understand this yourself. However, I think the information you need to get you past this problem is already available elsewhere and the link I posted might be a good starting point. – TypeIA Jan 20 '14 at 17:47
  • 1
    Try your program with 1. You'll see that something else happened. – francis Jan 20 '14 at 17:48
  • Then reverse it back. – herohuyongtao Jan 20 '14 at 17:48
  • @dvnrrs thank you very much i appreciate the help for it what bothers me is that i can't use arrays but i'll check the link and see if i can resolve it – user2985083 Jan 20 '14 at 17:49
  • @herohuyongtao i don't know how to reverse it back – user2985083 Jan 20 '14 at 17:50
  • @user2985083: Some things to note. Because `rem == 1` or `rem == 2`, we know that `rem % 10 == rem` and that `rem / 10 == 0`. Also, in the context of your particular while loop, you throw away `rem /= 10` anyways. – Bill Lynch Jan 20 '14 at 17:51
  • @user2985083: And for whatever reason, my brain isn't working. We know that `rem == 0 or rem == 1`. It can't be 2. Sorry about that. But everything else I said is still true. – Bill Lynch Jan 20 '14 at 18:09

6 Answers6

2

You can do it the "Last In First Out" way:

void Print(int number,int base)
{
    if (number >= base)
        Print(number/base,base);
    printf("%d",number%base);
}

Then, from main, you can call it with any base between 2 and 10.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • Hey i'm still having problems with it. It's still not printing it correctly – user2985083 Jan 20 '14 at 21:43
  • Works fine for me. You probably need to add `printf("\n")` after every time you call `Print(...)`. – barak manos Jan 20 '14 at 21:55
  • You're not using `Print` correctly. All you have to do is: after you call `scanf("%d",&number)`, call `Print(number)`. That's all. BTW, make sure you are using the same type in `main` and in `Print` (either use `int` in both, or `long int` in both). In addition, set the format accordingly (`"%d"` if you're using `int` and `"%ld"` if you're using `long int`). – barak manos Jan 21 '14 at 13:36
0

you shall various way to solve the problem.

process 1: just do right shift, check shifted result and print decised output.

int main()
{
  int n, c, k;

  printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);

  printf("%d in binary number system is:\n", n);

  for (c = 31; c >= 0; c--)
  {
    k = n >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  }

  printf("\n");

  return 0;
}
java seeker
  • 1,246
  • 10
  • 13
  • Nice steal. :) You should leave references when you provide an answer like this, I know who wrote this code. – Secko Jan 20 '14 at 21:13
0

Try this code.

int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("%d",decimal_binary(number));


int decimal_binary(int n)  /* Function to convert decimal to binary.*/
{
int rem, i=1, binary=0;
while (n!=0)
{
    rem=n%2;
    n/=2;
    binary+=rem*i;
    i*=10;
}
return binary;
}
Secko
  • 7,664
  • 5
  • 31
  • 37
Ashish Ratan
  • 2,838
  • 1
  • 24
  • 50
  • This is so weird. It converts the number (which is already "in binary") to some kind of "decimal packed binary" form, where e.g. the input 47 is converted to the decimal number 11111, since that happens to also be the binary representation of the number. So very confusing. – unwind Jan 20 '14 at 17:55
0

I'm trying to convert decimal to binary

You don't have anything decimal past the point you have scanned the input. You have a number. Numbers are not decimal or binary, they are just numbers. scanf has converted a decimal representation of some number to just a number for you.

Once you have a number you want to obtain its binary representstion. Once again, you have nothing decimal at hand.

Now ask yourself whether division by 10 you have found at some dark corner of the Internet could possibly be correct here.

(This answer is intentionally left unfinished)

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

long int decimalNumber,remainder,quotient;

int binaryNumber[100],i=1,j;


printf("Enter any decimal number: ");

scanf("%ld",&decimalNumber);


quotient = decimalNumber;


while(quotient!=0){

     binaryNumber[i++]= quotient % 2;

     quotient = quotient / 2;

}


printf("Equivalent binary value of decimal number %d: ",decimalNumber);

for(j = i -1 ;j> 0;j--)

     printf("%d",binaryNumber[j]);
Ashish Ratan
  • 2,838
  • 1
  • 24
  • 50
0

Here you go from your own code I was able to correct misleads and provide a correct display of the binary number.

#include<stdio.h>

int main()
{
    long int decimal, quotient;
    int binarynumber[1024];
    const int base = 2;

    printf("Enter a number: ");
    scanf("%ld", &decimal);
    printf("%d in binary: ", decimal);

    quotient = decimal;

    int i=1;
    while (quotient!=0)
    {
        binarynumber[i++]=quotient%base;
        quotient/=base;
    }
    for(int j=i; j>0; --j){
        printf("%d",binarynumber[j]);
    }
    printf("\n");
    return 0;
}

You were correct to have a decimal number variable, I called it decimal to avoid confusion. However, you can just store the input of the decimal number inside of quotient, it will work just the same.

I added the base number variable base, which should be a constant in my opinion, instead of the modulus operator plus base, which is %2 in your code.

You should have a variable, you had rem, binarynumber in this code - to store it all in and then reverse the numbers in this variable like in the for loop provided in the code. I though it better to store it in an array instead of creating a confusion with an additional variable called rem.

I don't entierly know what you wanted to do with rem/=10;, so I worked around that.

You were correct to use long int for the quotient, however, long int in many cases, is the same as int.

On many (but not all) C and C++ implementations, a long is larger than an int. Most compilers use a 32 bit int which has the same size and representation as a long. Reference

One more thing, you used to divide by base and store into the quotient (in your code quotient=quotient/2;) before retrieving the modulus answer, which resulted in a incorrect modulus, an incorrect remainder that is (in your code rem=quotient%2;). It retrieved the reminder of the next number.

Hope this is clear.

Secko
  • 7,664
  • 5
  • 31
  • 37
  • Hi there first of all thank you so very much for your help. The challenge here is not doing it with arrays. I'm trying to figure out a way to do it without using arrays.Though i know how to do it with them. but thank you for your answer – user2985083 Jan 20 '14 at 21:40
  • @user2985083 You didn't state that in you question. I will try to update once I come up with a solution. – Secko Jan 26 '14 at 23:46