Lab Assignment: Convert to Reverse Binary

Categories: Technology

Introduction

This lab assignment aims to create a Java program that takes a positive integer as input and converts it into reverse binary representation.

Get to Know The Price Estimate For Your Paper
Topic
Number of pages
Email Invalid email

By clicking “Check Writers’ Offers”, you agree to our terms of service and privacy policy. We’ll occasionally send you promo and account related email

"You must agree to out terms of services and privacy policy"
Write my paper

You won’t be charged yet!

Get quality help now
Doctor Jennifer
Doctor Jennifer
checked Verified writer

Proficient in: Technology

star star star star 5 (893)

“ Thank you so much for accepting my assignment the night before it was due. I look forward to working with you moving forward ”

avatar avatar avatar
+84 relevant experts are online
Hire writer

In reverse binary representation, the bits are output in reverse order. The algorithm used for this conversion is as follows:

  1. While the input integer (x) is greater than 0:
  2. Output the remainder of x % 2 (either 0 or 1).
  3. Update x to x / 2 (integer division).
  4. Repeat steps 1-3 until x becomes 0.

Program Code

Below is the Java program that implements the algorithm described above:


import java.util.Scanner;
public class Lab2_1 {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
    System.out.print("Please type in a positive integer: ");
    int integer = stdin.nextInt();
    String binary = "";

    while (integer > 0) {
        binary += integer % 2;
        integer /= 2;
    }

    System.out.println("Reverse Binary is " + binary);
}
}

Explanation of the Code

The Java program begins by importing the Scanner class to facilitate user input.

It then defines the Lab2_1 class with a main method. Inside the main method:

  1. A Scanner object named stdin is created to read user input.
  2. The user is prompted to enter a positive integer using System.out.print.
  3. The entered integer is stored in the variable integer.
  4. A string variable binary is initialized to an empty string to store the reverse binary representation.
  5. A while loop is used to implement the reverse binary conversion algorithm:
    • The remainder of integer % 2 (either 0 or 1) is appended to the binary string.
    • The value of integer is updated to integer / 2 using integer division.
    • This process continues until integer becomes 0.
  6. Finally, the reverse binary representation stored in binary is printed to the console using System.out.println.

Example

For instance, if the user enters the input:
6

The program will produce the following output:


Please type in a positive integer: 6
Reverse Binary is 011

Explanation: The input integer 6 is converted to binary as 110, but since we are using reverse binary, the program outputs 011.

Conclusion

This lab assignment has successfully demonstrated how to create a Java program that converts a positive integer into reverse binary representation. The program utilizes a straightforward algorithm and user input to generate the desired output. Understanding binary representation and programming logic is crucial for such tasks, and this assignment serves as a practical example of these concepts.

Updated: Jan 03, 2024
Cite this page

Lab Assignment: Convert to Reverse Binary. (2023, Aug 04). Retrieved from https://studymoose.com/document/comp-182-lab-assignment-2-1-convert-to-reverse-binary

Lab Assignment: Convert to Reverse Binary essay
Live chat  with support 24/7

👋 Hi! I’m your smart assistant Amy!

Don’t know where to start? Type your requirements and I’ll connect you to an academic expert within 3 minutes.

get help with your assignment