design an application that repeatedly accepts an 8 bit binary number from the us
ID: 3831539 • Letter: D
Question
design an application that repeatedly accepts an 8 bit binary number from the user, the application then provides back the decimal value of the number provided. the application asks the user for next number computation. Y for continuation and n for termination(C++ structure code )
design an application that repeatedly accepts an 8 bit binary number from the user, the application then provides back the decimal value of the number provided. the application asks the user for next number computation. Y for continuation and n for termination
(C++ structure code )
design an application that repeatedly accepts an 8 bit binary number from the user, the application then provides back the decimal value of the number provided. the application asks the user for next number computation. Y for continuation and n for termination
(C++ structure code )
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
int main()
{
long binary, decimal = 0,
long rem, number, base = 1;
char ch;
string input;
ch = 's';
cout << "Enter Y to continue and N to quit" << endl;
cin >> ch;
while (ch == 'Y'){
cout << "Enter the binary number(in 1s and 0s) : ";
cin >> input;
if (input.length() != 8) {
cout << "Please enter 8 bits" << endl;
continue;
}
number = atol(input);
binary = number;
while (number > 0)
{
rem = number % 10;
decimal = decimal + rem * base;
base = base * 2;
number = number / 10;
}
cout << "The decimal equivalent of " << binary << " : " << decimal << endl;
cout << "Enter Y to continue and N to quit" << endl;
cin >> ch;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.