C Program that counts in 8-bit Binary ******************************************
ID: 3862702 • Letter: C
Question
C Program that counts in 8-bit Binary
*************************************************
Write a program in C that taks as an input an 8-bit binary number and prints the next 10 binary numnbers.
-Define a binary number as int binNum[8]
Use binNum[0] to store the leftmost bit and binNum[7] t ostore the last rightmost bit.
Ask the user to input the first binary number with each bit seperate by at least one space
**************************************************
Test program with input 1 0 1 1 0 0 0 1 and out put the following 10 8-bit binary digits following this one.
Explanation / Answer
#include <stdio.h>
int main()
{
int binNum[8]; // Array to read binary number
long dec=0,n=0; // variables used to convert binary to decimal
int k=0,l=0;
long binary=0;
int i=1,j=0,remainder=0;
//reading the binary number in to the array binNum
printf("Please Enter the first binary number with each bit seperate by at least one space : ");
scanf("%d %d %d %d %d %d %d %d",&binNum[0],&binNum[1],&binNum[2],&binNum[3],&binNum[4],&binNum[5],&binNum[6],&binNum[7]);
//converting the binary number in to decimal
for(k=7;k>=0;k--)
{
dec=(binNum[k]*power(2,l))+dec;
l++;
}
printf("Next 10 binary numbers are as below : ");
//Printing next 10 binary numbers by incrementing the decimal number.
//and converting the decimal number to binary after increment
for(j=1;j<=10;j++,dec++){
i=1;
remainder=0;
binary=0;
n=dec+1;
while(n != 0) {
remainder = n%2;
n = n/2;
binary= binary + (remainder*i);
i = i*10;
}
printf("%ld ",binary);
}
return 0;
}
//Power function to calculate the power of 2 numbers.
int power(int c, int d)
{
int pow=1;
int i=1;
while(i<=d)
{
pow=pow*c;
i++;
}
return pow;
}
/*
Test Case :-
Please Enter the first binary number with each bit seperate by at least one space :
1 0 1 1 0 0 0 1
Next 10 binary numbers are as below :
10110010
10110011
10110100
10110101
10110110
10110111
10111000
10111001
10111010
10111011
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.