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, . . . .
c# language Write a program that takes in a number from the user. Then it prints...
c# language Write a program that takes in a number from the user. Then it prints a statement telling the user if the number is even or odd. If the number is odd, it counts down from the number to 0 and prints the countdown on the screen, each number on a new line. If the number is even, it counts down from the number to 0, only even numbers. For example, if the user enters 5, the output will...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Important! Consider which control structures will work best for which aspect of the assignment. For example, which would be the best to use for a menu?...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement...
5. Write a CH program that takes the marks of a student as input and prints...
5. Write a CH program that takes the marks of a student as input and prints the grade on screen according to the following criteria: CRITERIA LESS THAN 60 GREATER THAN 60 BUT LESS THAN 65 GREATER THAN 65 BUT LESS THAN 70 GREATER THAN 70 BUT LESS THAN 75 GREATER THAN 75 BUT LESS THAN 80 GREATER THAN 80 BUT LESS THAN 85 GREATER THAN 85 BUT LESS THAN 90 GREATER THAN 90 GRADE F D D+ с C+...
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...
Problem: Write a program that takes your weight in pounds as input and then prints how...
Problem: Write a program that takes your weight in pounds as input and then prints how much you will weigh on Moon and Mars. The formula to convert weight on the Earth to weight on Moon and Mars are given below: Weight on Moon = weight on Earth * 0.165 Weight on Mars = weight on Earth * 3.711 / 9.81 You should name the program as weight_watcher.py. The output should look like as shown below:
(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:...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT