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 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.
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...
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)
The following question must be answered in the C programming language and may not be written...
The following question must be answered in the C programming language and may not be written in C++ or any other variation. Problem 5 This problem is designed to make sure you can write a program that swaps data passed into it such that the caller's data has been swapped. This is something that is done very frequently in manipulating Data Structures. The Solution / Test Requirements I want your program to demonstrate that you understand how to swap information...
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
Write an application that prints a table of the binary and octal equivalent of the decimal...
Write an application that prints a table of the binary and octal equivalent of the decimal numbers in the range 1 through 256. **Write in JAVA**
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT