Java Program to Check if a Number is a Multiple of 5 Without Using / and % Operators13 May 2025 | 4 min read Given a number n. The task is to check if a number is a multiple of 5 without using the division (/) or modulus (%) operators. Example 1: Input: 30 Output: 30 is a multiple of 5: true Explanation: The last digit of 30 is 0, so it is a multiple of 5. Example 2: Input: 17 Output: 17 is a multiple of 5: false Explanation: The last digit of 17 is 7, which is not 0 or 5, so it's not a multiple of 5. Example 3: Input: 125 Output: 125 is a multiple of 5: true Explanation: The last digit of 125 is 5, so it is a multiple of 5. Approach 1: Using Repeated SubtractionThe approach determines whether a given number is a multiple of 5 by repeatedly subtracting 5 from it. If the number eventually reduces to 0, it is a multiple of 5; otherwise, it is not. AlgorithmStep 1: Convert the number to its absolute value to handle negative numbers. Step 2: Repeatedly subtract 5 from the number until it is less than 5. Step 3: If the remaining value is 0, return true (it is a multiple of 5); otherwise, return false. Output: 25 is a multiple of 5: true ComplexityTime Complexity: The time complexity of a program is O(n). Because we repeatedly subtract 5, so for a large n, the loop runs approximately n/5 times. Space Complexity: The space complexity of a program is O(1). Because no extra space is used apart from a few integer variables. Approach 2: Using Multiplication and SubtractionThe approach checks if a number is a multiple of 5 by iterating from 0 and multiplying by 5 until we either reach the given number or surpass it. If we find a match, the number is a multiple of 5; otherwise, it is not. AlgorithmStep 1: Take the absolute value of the number to handle negative cases. Step 2: Initialize x = 0. Step 3: Use a loop to compute 5 * x and check if it matches n: Step 3.1: If 5 * x == n, return true. Step 3.2: If 5 * x exceeds n, return false. Step 3.3: Otherwise, increment x and continue. Output: 20 is a multiple of 5: true ComplexityTime Complexity: The time complexity of a program is O(n). Because we iterate a loop to compute. Space Complexity: The space complexity of a program is O(1). Because no extra space is used apart from a few integer variables. Approach 3: Convert Number to String and Check Last DigitThe approach converts the given number into a string and checks if the last digit is 0 or 5. Since numbers that are multiples of 5 always end in 0 or 5. AlgorithmStep 1: Convert the number to its absolute value (to handle negatives). Step 2: Convert the absolute number to a string. Step 3: Extract the last digit using charAt(length - 1). Step 4: If the last digit is '0' or '5', return true; otherwise, return false. Output: 20 is a multiple of 5: true ComplexityTime Complexity: The time complexity of a program is O(n). Because we use a string conversion and last-digit checking are quick operations. Space Complexity: The space complexity of a program is O(1). Because only a string and a character variable are used. Next TopicJava 8 filters |
In the competitive programming, using efficient and reliable libraries truly makes a huge difference in productivity and performance. In this tutorial, we will be focusing on the most important containers from the Collection Framework. Java Standard Library contains the following data structures: 1. ArrayList ArrayList is a part of...
24 min read
In this section, we will learn about finding the possible paths from top left to bottom right of a matrix in Java. It is one of the prominent problems asked in the interview. The constraint to go from top left to bottom right is that from...
5 min read
The bully algorithm is a type of Election algorithm which is mainly used for choosing a coordinate. In a distributed system, we need some election algorithms such as bully and ring to get a coordinator that performs functions needed by other processes. Election algorithms select a single...
4 min read
Agile software development has gained immense popularity in recent years due to its flexibility, customer-centric approach, and iterative development practices. Java, being one of the most widely used programming languages, aligns seamlessly with Agile methodologies. In this section, we will explore Agile principles, patterns, and practices...
4 min read
Hash tables are a fundamental data structure in computer science, providing efficient storage and retrieval of key-value pairs. They achieve average-case constant time complexity for search, insert, and delete operations, making them highly valuable for various applications such as database indexing, caching, and associative arrays....
6 min read
Arrays are also among the most fundamental, easiest, and simplest data structures in Java and many other languages. They help a developer store a number of values of the same kind in a single block of memory that is contiguous. Therefore, this makes access and...
6 min read
In this section, we will learn what is Pig Latin word and how to translate or encode a word into a Pig Latin word. Also, we will implement the logic in a JavaM program to find the Pig Latin string. What is Pig Latin? Pig Latin is a...
3 min read
in Java The longest subsequence common to all the given sequences is referred to as . The reason for using the LCS is to restrict the element of the subsequences from occupying the consecutive position within the original sequences. A sequence that appears in the same relative...
4 min read
Java, a versatile and popular programming language, offers a wide range of tools and data structures to help developers create efficient, reliable, and thread-safe applications. One such tool in the Java Concurrency framework is the Atomic Boolean. In this section, we will explore what is Atomic...
16 min read
The FileInputStream class of the Java programming language is used to read data from files in a byte-oriented fashion. It has several data reading methods, including read(), read(byte[]), and read(byte[], int, int). Finalise(), a method that the FileInputStream class inherits from the Object class, is one...
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