Java Program to Check if Binary Number is Multiple of 36 May 2025 | 4 min read Binary numbers play a vital role computer science. It shows information using only the numbers 0 and 1. Determining whether a binary number is divisible by 3 or not is an understanding of modular arithmetic, numbers and multipliers is required. A binary number can be analyzed to determine divisibility by converting it to its decimal equivalent or directly evaluating its properties using rules based on modulus arithmetic. Core Concept Behind the SolutionThe algorithm for checking whether a binary number is divisible by 3 depends on the properties of the parameter operation. The binary numbers dn , dn-1 can be considered as strings of numbers …. d1, d0 where di {0, 1}. The decimal equivalent is calculated as: ![]() Using modulus properties, we can avoid converting the entire binary number into decimal form. Instead, we evaluate the number iteratively while keeping track of its remainders when divided by 3. Observations
ExampleInput: 110
Output: The binary number is not a multiple of 3. Let's implement the above approach in a Java program. File Name: BinaryMultipleOfThree.java Output: Enter a binary number: 110 The binary number is not a multiple of 3. ExplanationThe Java program checks if a binary number is divisible by 3 using a finite state machine (FSM) approach. It processes the binary string bit by bit, tracking the remainder when divided by 3 using a state variable (0, 1, 2). Starting from the initial state 0, the program transitions between states based on the current bit (0 or 1). For example, a 1 advances the state (for example, 0 to 1), while a 0 keeps it unchanged. Invalid input (non-binary characters) is handled by throwing an exception. After processing all bits, if the state remains 0, the binary number is divisible by 3. It avoids converting the binary string to decimal, ensuring efficiency and scalability. Key Features of the Program
Advantages of the Approach
Complexity AnalysisTime Complexity: The algorithm runs in (O(n)), where (n) is the length of the binary string. Each bit is processed exactly once, making the operation linear. Space Complexity: The program uses (O(1)) space since it maintains only a single state variable and does not require additional storage proportional to the input size. ConclusionThe program efficiently determines if a binary number is a multiple of 3 using a simple yet powerful FSM-based approach. By leveraging modular arithmetic, it eliminates the need for large-number computations, making it robust and suitable for handling long binary strings. Its linear time complexity ensures fast execution, while its low space requirement guarantees minimal resource usage. This method exemplifies how mathematical insights can lead to elegant and practical algorithm designs. |
Java's bit manipulation allows us to store two numbers in a single byte while streamlining our code. The technique of changing individual bits in a binary data representation is called bit manipulation. In this situation, bit manipulation can be used to condense two numbers into a...
4 min read
Given a string 'str' with length N. The task is to find the greatest lexicographic string in which we can only move a single character from 'str' to any other index once. Example 1: Input: String str = "cad" int N = 3 Output: The Largest Lexicographic string is dca Explanation: The string's length...
4 min read
Hamming code is a special code in a Computer network, which is a set of error-correction codes. In Computer Graphics, it is mainly used for detecting and correcting errors that occur at the time of data transmission from sender to receiver. In Java, we can implement...
6 min read
What are Left-Truncatable Prime? In number theory, a left-truncatable prime is a special type of prime number that remains prime when the leading digits are successively removed. In other words, if we truncate the leftmost digit of a left-truncatable prime, the resulting number is still a prime. The...
3 min read
Java, being an object-oriented programming language, places a strong emphasis on resource management. One critical aspect of this is ensuring that resources like file handles, database connections, and network connections are properly released when they are no longer needed. The AutoCloseable interface plays a pivotal role...
3 min read
In a binary tree, the greatest difference between a node and its ancestor is the highest value achieved by subtracting the value of a descendant node from one of its ancestor nodes. An ancestor of a node is any node located along the route from the...
5 min read
The Rabin-Karp Algorithm is an efficient string-matching method that uses hashing to find a pattern within a text. Rather than examining each character individually, it computes a hash value for the pattern and compares it with the hash values of text substrings. When a hash match...
6 min read
The short-circuit operator is used to optimize conditional expressions by evaluating only the necessary components, which can result in improved performance. In Java, the short-circuit operator consists of two symbols: "&&" for logical AND and "||" for logical OR. These operators are primarily used in conditional...
6 min read
Java, a versatile and widely used programming language, is renowned for its object-oriented approach and platform independence. One of the key features contributing to the language's organizational prowess is the concept of packages. In Java, packages serve as containers for classes, providing a structured way to...
6 min read
In this section, we will learn what is a sphenic number and also create Java programs to check if the given number is sphenic or not. The sphenic number program frequently asked in Java coding tests and academics. Sphenic Number A positive integer n is called a sphenic...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India