Write a program to ask the user to input a binary number (largest is 1111111111)
ID: 3590587 • Letter: W
Question
Write a program to ask the user to input a binary number (largest is 1111111111), such as 1001 which equals to decimal number 9. Use int variable to store the binary number. Then, convert the binary number into decimal integer format and print the decimal integer out. You should check whether the number the user input is a binary number or not, if not, you should keep asking for correct input. Name your file Hw3_q3_code.c (TIP: binary number bnbn-1bn-2 b2b1 = bn * 2n-1 + bn-1 * 2n-2 + + b2 * 21 + bi * 2° , e.g. 1001 = 1 * 23 + 0 * 22 + 0 * 21 + 1 * 2"-9 . Use mod %2, and divide a 0, alternatively to decompose the binary number, for example translate the binary number 101: 1. 2. 3. decimal-num=0; 101 % 2 = I ; decimal-num += 1 * 20; 101 / 10-10; 10%2-0; decimal num += 0 * 2^ 1:10/10-1; 1 % 2 = 1 ; decimal-num = 1 * 2"2; 1/10-0; Decimalnum=5. End. 4. 5. - Use ‘%10, in each iteration to test teat whether the input number is binary or not.)Explanation / Answer
/*The comments and the explantion of the code is given along side the code.
The basic logic for converting binary to decimal is multiplying the digits
with the base and adding it to the number, and changing the base by multiplying
with 2, Bases are powers of 2 binary numbers. Bases change as 1,2,4,8,16,32,64
for each digit if binary number starting from the rightmost digit.*/
#include <stdio.h>
int userInput();
void calculateDecimal(int num);
void main()
{
/*checks if the value is -1 or not
-1 indicates a wrong input
*/
int result=userInput();
while(result==-1){
result=userInput();//userInput function is called again and again till the user gives a correct input
}
}
int userInput(){
int num,numb,rem;
//Input message
printf("Enter a binary number: ");
scanf("%d", &num);//taking user input
numb=num;//Storing user input
while (num > 0)
{
rem = num % 10;
if(rem!=0 && rem!=1){//checking for a valid binary number
return -1;
}
num=num/10;
}
calculateDecimal(numb);//calculating the decimal equivalent of the number
return 1;
}
void calculateDecimal(int num){
int binary;
int decimal = 0;//decimal number
int base = 1;//base starts with 1 and continues with powers of 2
int rem;//remainder for the storing the digits one by one
binary = num;
while (num > 0)
{
rem = num % 10;//taking out the last digit
decimal = decimal + rem * base;//multiplying with base and adding to decimal
num = num / 10 ;//remaining number except the last digit
base = base * 2;//changing the base to the next base 1,2,4,8,16,32,64 and so on
}
printf("The Binary number is = %d ", binary);//the decimal number
printf("The decimal equivalent is = %d ", decimal);//the binary number
}
/*
Sample input:
103
10010
Sample output:
Enter a binary number:
Enter a binary number:
The Binary number is = 10010
The decimal equivalent is = 18
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.