Question
I'm having some trouble converting decimal number into binary numbers but no matter what number i type in i get the same output 0 (base-10)
I'm having some trouble converting decimal number into binary numbers but no matter what number i type in i get the same output
0 (base-10) is equivalent to 1 (base-2)!
0 (base-10) is equivalent to 0 (base-2)!
Here's my code :
#include
int main(){
int i;
int num;
int binary[i];
printf("Starting the CPSC 1011 Decimal to Binary Converter ");
printf("Please enter a postive whole number(or EOF to quit):" );
scanf("%d", &num);
if(num<=0){
printf("Sorry, that was not a postive whole number.");
printf(" ");
}
else{
while(num>0){
binary[i]=num%2;
num=num/2;
i++;
}
for(int j=i-1; j>=0; j--){
printf("%d (base-10) is equivalent to %d (base-2)! ", num, binary[j]);
}
}
return(0);
}
This is the output I'm trying to get:
Starting the CPSC 1011 Decimal to Binary Converter! Please enter a positive whole number (or EOF to quit): 42 42 (base-10) is equivalent to 101010 (base-2)! Please enter a positive whole number (or EOF to quit): -2 Sorry, that was not a positive whole number. Please enter a positive whole number (or EOF to quit): 1.1 Sorry, that was not a positive whole number. Please enter a positive whole number (or EOF to quit): 11.0 11 (base-10) is equivalent to 1011 (base-2)! Please enter a positive whole number (or EOF to quit): Thank you for using the CPSC 1011 Decimal to Binary Generator. Goodbye! |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started