Question

In: Computer Science

ite a C program that prompts for and reads in a non-negative integer from the keyboard....

ite a C program that prompts for and reads in a non-negative
   integer from the keyboard. Read it into an int variable x. Then
   display the 32 bits in x from the lsb to the msb (so the display
   will show the value in x in binary with the bits in reverse
   order).  For example, if you input 6, then your program displays

     00000000000000000000000000000110

   Use the algorithm given in class (repeatedly divide by 2). Use 
   the / and % operators. Because this algorithm determines the 
   bits in right-to-left order and you have to display the bits 
   in left-to-right order, you first have to determine and save 
   all the bits, then display them. To save the bits, save them 
   in an int array named bits: 

   int bits[32];

   The following statement assigns the remainder to the ith element
   of the array:

       bits[i] = quotient % 2;

Solutions

Expert Solution

Program:

#include<stdio.h>
int main()
{
   int n, i=0,remainder=0, q=0, bits[32];
   printf("Enter a non negative integer: ");
   scanf("%d",&n);
   q=n; // assigned the value of n to another integer so that the original value does not change. You can avoid this assignment also.
   while(q>0)
   {
bits[i++]=q%2; // calculating the remainder dividing by 2 for finding binary and store in the bits array
       q=q/2; // the quotient is stored for next iteration
  
   }
   while(i<32) // the calculated binary value may be less than 32 bits. So the remaining bits are set to 0
   {
       bits[i]=0;
       i++;
   }
   for(i=31;i>=0;i-- ) //the bits array is pronted in reverse order
   {
       printf("%d",bits[i]);
   }
   return 0;
}

Output

Without assigning the value of the integer n to q you can also use this code it will also run fine

Program:

#include<stdio.h>
int main()
{
   int n, i=0,remainder=0, bits[32];
   printf("Enter a non negative integer: ");
   scanf("%d",&n);
  
   while(n>0)
   {
       bits[i++]=n%2; // calculating the remainder dividing by 2 for finding binary and store in the bits array
       n=n/2; // the quotient is stored for next iteration
     
   }
   while(i<32) // the calculated binary value may be less than 32 bits. So the remaining bits are set to 0
   {
       bits[i]=0;
       i++;
   }
   for(i=31;i>=0;i-- ) //the bits array is pronted in reverse order
   {
       printf("%d",bits[i]);
   }
   return 0;
}

Output:

N.B: If you face any difficulty please contact through comment. Otherwise please give your feedback.


Related Solutions

(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print out "n" lines as follows: 0 0 1 0 1 2 0 1 2 3 … 0 1 2 3 … n-1
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
In C++, The following program reads one character from the keyboard and will display the character...
In C++, The following program reads one character from the keyboard and will display the character in uppercase if it is lowercase and does the opposite when the character is in uppercase. If the character is a digit, it displays a message with the digit. Modify the program below such that if one of the whitespaces is entered, it displays a message and tells what the character was. // This program reads one character from the keyboard and will //...
Create a CubesSum application that prompts the user for a non-negative integer and then displays the...
Create a CubesSum application that prompts the user for a non-negative integer and then displays the sum of the cubes of the digits.   b) Modify the application to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits.(Java Programming )
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and...
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and a base in the range 2 <= base <= 16. Write a function multibaseOutput() that displays the number in the specified base. The program terminates when the user enters a number of 0 and a base 0. Run: Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: 155 16     155 base 16 is 9B Enter...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
Question 1: Write a C program that reads a date from the keyboard and tests whether...
Question 1: Write a C program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid month...
In C ++, Design and implement a program that reads from the user four integer values...
In C ++, Design and implement a program that reads from the user four integer values between 0 and 100, representing grades. The program then, on separate lines, prints out the entered grades followed by the highest grade, lowest grade, and averages of all four grades. Format the outputs following the sample runs below. Sample run 1: You entered:    95, 80, 100, 70 Highest grade: 100 Lowest grade:   70 Average grade: 86.25
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT