Write an application that inputs an integer containing only 0s and 1s (i.e., bin
ID: 670666 • Letter: W
Question
Write an application that inputs an integer containing only 0s and 1s (i.e., binary integer) and prints its decimal equivalent. [Hint: Use the remainder and division operators to pick off the binary number's digits one at a time, from right to left. In decimal number system, the rightmost digit has a positional value of 1 and the next digit to the left a positional vlaue of 10, the 100, then 1000, and so on. The decimal number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. In the binary number system, the rightmost digit has a positional value of 1, the next digit to the left a positional value of 2, then 4, then 8, and so on. The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8, or 13.]
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int bi,sum=0,i=0,a;
cout<<"Enter no";
cin>>bi;
while(bi>0)
{
a=bi%10;
sum=sum+(a*pow(2,i));
bi=bi/10;
i++;
}
cout<<sum;
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.