I need help writing a C++ program that takes an eight-bit binary number and prin
ID: 3662645 • Letter: I
Question
I need help writing a C++ program that takes an eight-bit binary number and prints the next 10 binary numbers. For example if the user enters 11111101 the output to the console should be 11111101
11111110
11111111
00000000
00000001
00000010
00000011
00000100
00000101
00000110
And if each bit entered by the user is not 0 or 1 it outputs an error message. I am defining the binary number as int binNum[8], using binNum[0] to store the kmost significant (leftmost) bit and binNum[7] the least significant bit. I have to ask the user to input the first binary number with each bit seperated by at least one space
Explanation / Answer
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void add(int binNum[])
{
int i,c=1;
for(i=7;i>=0;i--)
{
binNum[i] +=c;
if(binNum[i]==2){binNum[i]=0;c=1;}
else c=0;
if(c==0)break;
}
}
int main()
{
int binNum[8],i,j;
char c;
cout<<"Enter 8 digit binary number:(Note: give space between each digit):";
for(i=0;i<8;i++)
{
cin>>binNum[i];
if(binNum[i]!=0&&binNum[i]!=1)
{ cout<<"Error: Digits must be 0 or 1";exit(0);}
}
for(j=0;j<10;j++)
{
for(i=0;i<8;i++){cout<<binNum[i];}
add(binNum);
cout<<" ";
}
return 0;
}