Hello. I'm currently trying to write a program that will convert a decimal number (0 ~ 65535) i insert into binary numbers. The problem is i want the conversion to happen using the principle of a Successive Approximation type A-D converter, where the process of conversion will be showed on the screen.
For example: when i enter number 103 for conversion, the output will be:
1000000
1100000
1100000
1100000
1100100
1100110
1100111
with the final row 1100111 as the answer. The process will be concluded in a maximum 32 steps.
So far, i have only managed to write a program with direct conversion without the process of converting showing on the screen.
Code:
#include<stdio.h>
int main(void){
int num,weight=1;
printf("Insert a positive number:");
scanf("%d",&num);
printf("Change %d to binary\n",num);
while(num-weight*2>=0){
weight *= 2;
}
while(weight){
printf("%d",num-weight>=0);
if(num-weight>=0){
num -= weight;
}
weight /= 2;
}
puts("");
return 0;
}
Thank you for your time. This is my first post here. I'm still a beginner in programming and sorry for my bad english. Please help.