#include <math.h>
#include <stdio.h>
void main() {
int decimal, count, binary, digit;
printf("Enter the number : ");
scanf("%d", &decimal);
count = 0; binary = 0;
while (decimal > 0) {
digit = decimal % 2;
binary = binary + digit * pow(10, count);
decimal = decimal / 2;
++count;
}
printf("Binary form : %d", binary);
}
I used the above code to convert Decimal to binary. However, the problem is the output.
Input : 12
Expected Output : 1100
Actual Output : 1099
[img]https://i.stack.imgur.com/4rMHt.png
This problem persists for other inputs too. Only 8 gives the correct output.
So can someone explain why this happens? This error also shows up in C++, when I port it there.
PS: This error also pops up while using pow
in checking if a number is Armstrong and if its a palindrome.