Question

In: Computer Science

THE FOLLOWING QUESTION IS FOR C PROGRAMMING LANGUAGE Printing the decimal equivalent of a binary number....

THE FOLLOWING QUESTION IS FOR C PROGRAMMING LANGUAGE

Printing the decimal equivalent of a binary number. Write a program that accepts an integer (5 digits or fewer) containing only 0s and 1s (i.e., binary) and prints out its decimal equivalent using the remainder and division operator to select the "binary" digits one at a time. Make sure your input is tested for multiple options: incorrect characters, too many, too few, etc.

I need help making this program. No loops, if else, or while statements are allowed. Thank you!

Solutions

Expert Solution

#include <stdio.h>
// return 0 if numberis invalid and 1 if number is valid
int isInputValid(int number)
{
int temp;
if (number < 0)
{
printf("Opps!!! Number is Negative\n");
return 0;
}
else if (number > 99999)
{
printf("Opps!!! Number contain more than 5 digit\n");
return 0;
}
// if number is greater than highest possible valid number
else if (number > 11111)
{
printf("Opps!!! Invalid Number\n");
return 0;
}
else
{
while (number != 0)
{
// take last digit
temp = number % 10;
number = number / 10;
// check if digit is valid or not
if (!(temp == 0 || temp == 1)) {
printf("Opps!!! Invalid Number\n");
return 0;
}
}
}
return 1;
}

int convertToDecimal(int number){
int powerFactor = 1, temp, output = 0;
while (number != 0)
{
temp = number % 10;
number = number / 10;
output = output + temp*powerFactor;
powerFactor = powerFactor * 2;
}
return output;
}

int main() {
int number;
printf("Enter your number: ");
scanf("%d",&number);

if (isInputValid(number))
{
printf("Final Number: %d\n", convertToDecimal(number));
}
return 0;
}


Related Solutions

Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
ASSEMBLY LANGUAGE PROGRAMMING 1. The decimal equivalent of the signed 2’s complement 8-bit binary number 11010101B...
ASSEMBLY LANGUAGE PROGRAMMING 1. The decimal equivalent of the signed 2’s complement 8-bit binary number 11010101B is ______________. 2. The decimal equivalent of the unsigned 8-bit hex number 0B4H is ______________. 3. The value of the expression ‘H’ – ‘B’ is less than / equal to / greater than that of the expression ‘L’ – ‘C’. 4. If the .data segment contains declarations A BYTE 2 DUP (‘a’), ‘+’ B BYTE 3 DUP (‘b’), 0 C BYTE 4 DUP (‘c’),...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if (M <= N + 3 && (C == ‘N’ || C == ‘n’)) C = ‘0’; else C = ‘1’; Assume that M and N are 32-bit signed integer variables, and C is an 8-bit ASCII character variable. All variables are stored in memory, and all general-purpose registers are available for use.
a)The decimal equivalent of the signed 2’s complement 8-bit binary number 11010101B is ______________. b)The decimal...
a)The decimal equivalent of the signed 2’s complement 8-bit binary number 11010101B is ______________. b)The decimal equivalent of the unsigned 8-bit hex number 0B4H is ______________. c)The value of the expression ‘H’ – ‘B’ is less than / equal to / greater than that of the expression ‘L’ – ‘C’. d)If the .data segment contains declarations                         A    BYTE      2 DUP (‘a’), ‘+’ B    BYTE      3 DUP (‘b’), 0 C    BYTE      4 DUP (‘c’), ‘–’ D    BYTE     ...
1. Give the binary equivalent of the decimal number of 11.77 (assume fixed point, no more...
1. Give the binary equivalent of the decimal number of 11.77 (assume fixed point, no more than 6 bits left and right of the decimal point)
Give the binary equivalent of the decimal number of 11.77 (assume fixed point, no more than...
Give the binary equivalent of the decimal number of 11.77 (assume fixed point, no more than 6 bits left and right of the decimal point)
Give the binary equivalent of the decimal number of 19.9 (assume fixed point, no more than...
Give the binary equivalent of the decimal number of 19.9 (assume fixed point, no more than 6 bits left and right of the decimal point(show work)
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number...
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number to fractional binary representation. 1. 103.5625
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT