Question

In: Computer Science

Write a program in C that takes as input an 8-bit binary number and prints the...

Write a program in C that takes as input an 8-bit binary number and prints the next 10 binary

numbers. Define a binary number as

int binNum[8];

Use binNum[0] to store the most significant (i.e., leftmost) bit and binNum[7] to store the least

significant bit. Ask the user to input the first binary number with each bit separated by at

least one space.

Solutions

Expert Solution

#include<stdio.h>
int main()
{
    int binNum[8],no[9];
    int i,s=0,j;
    //input the binary number
    printf("Enter the binary number");
    for(i=0;i<8;i++)
    scanf("%d",&binNum[i]);
   
    //display the input number
    for(i=0;i<=7;i++)
   printf("%d ",binNum[i]);
   //condition when the 1ft 5 bit's are 1(from MSB)
   if(binNum[0]==1 && binNum[1]==1 && binNum[2]==1 && binNum[3]==1 && binNum[4]==1)
   {
      no[0]=0;
      for(j=1,i=0;i<8;i++,j++)
      no[j]=binNum[i];
        
      printf("\n Binary Numbers :\n");
    //loop to print the 10 next binary numbers
    for(j=1;j<=10;j++)
    {
       s=1; //assign s to 1 to generate next number
    for(i=8;i>=0;i--) //loop to scan each bit of binary number from LSB to MSB
    {
       if(no[i]==0) //if bit is 0 then add 1 to that position
       {
       no[i]= no[i]+s;
       s=0; //set s to 0
       break;   //terminate the loop
       }
      
       else
       if(no[i]==1 && s==1 ) //if bit is 1 and s=1 theh set the bit to 0 and set s to 1
       {
          no[i]=0;
          s=1; //carry forward bit
           }
   }
   printf("\n");
   //display the next number
   if(no[0]==0) //if the 9th bit is 0 (MSB)
   {
      for(i=1;i<=8;i++)
   printf("%d ",no[i]);
   }
   else
   for(i=0;i<=8;i++)
   printf("%d ",no[i]);
   }
       }
       else
       {
          printf("\n Binary Numbers :\n");
    //loop to print the 10 next binary numbers
    for(j=1;j<=10;j++)
    {
       s=1; //assign s to 1 to generate next number
    for(i=7;i>=0;i--) //loop to scan each bit of binary number from LSB to MSB
    {
       if(binNum[i]==0) //if bit is 0 then add 1 to that position
       {
       binNum[i]= binNum[i]+s;
       s=0; //set s to 0
       break;   //terminate the loop
       }
      
       else
       if(binNum[i]==1 && s==1 ) //if bit is 1 and s=1 theh set the bit to 0 and set s to 1
       {
          binNum[i]=0;
          s=1; //carry forward bit
           }
   }
   printf("\n");
   //display the next number
   for(i=0;i<=7;i++)
   printf("%d ",binNum[i]);
   }
       }
   
       
  
  
}

OUTPUT


Related Solutions

Write a program in C that takes as input a four-digit hexadecimal number and prints the...
Write a program in C that takes as input a four-digit hexadecimal number and prints the next 10 hexadecimal numbers. Define a hexadecimal number as int hexNum[4] Allow upper- or lowercase letters for input and use uppercase letters for the hexadecimal output. For example, 3C6f should be valid input and should produce output 3C6F, 3C70, 3C71, . . . .
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Text Wrap Problem Write a program in Python that takes an input string and prints it...
Text Wrap Problem Write a program in Python that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole. For example, the first line of the Gettysburg address: Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal Becomes: Four score and...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The user will type in two-4 bit binary numbers with the selection of one of the operations. Then, the program will calculate the result of the calculation. Display two-4 bit binary numbers and the result from the calculation.
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
Write a C program that, given a file named Program_2.dat as input, determines and prints the...
Write a C program that, given a file named Program_2.dat as input, determines and prints the following information: The number of characters in the file. The number of uppercase letters in the file. The number of lowercase letters in the file. The number of words in the file. The number of lines in the file. Your program should assume that the input file, Program_2.dat, may contain any text whatsoever, and that text might be, or might not be, the excerpt...
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT