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 )
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.
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Problem description Write a C++ program that prompts the user to enter two non-negative integers, firstNum...
Problem description Write a C++ program that prompts the user to enter two non-negative integers, firstNum and secondNum. The program then prints all palindromic primes (Links to an external site.) between firstNum and secondNum, inclusive. A palindromic number is a number whose digits are the same forward or backward (e.g., 12321). A palindromic prime is a prime number that is also a palindromic number (e.g., 101). You must implement and use the following functions as prototyped below: /// --------------------------------------------------------------- ///...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT