Skip to content
Related Articles

Related Articles

Check if a number is multiple of 5 without using / and % operators

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 05 May, 2022

Given a positive number n, write a function isMultipleof5(int n) that returns true if n is multiple of 5, otherwise false. You are not allowed to use % and / operators

 

Method 1 (Repeatedly subtract 5 from n) 
Run a loop and subtract 5 from n in the loop while n is greater than 0. After the loop terminates, check whether n is 0. If n becomes 0 then n is multiple of 5, otherwise not. 

Below is the implementation of the above approach:

C++




#include <iostream>
using namespace std;
 
// assumes that n is a positive integer
bool isMultipleof5 (int n)
{
    while ( n > 0 )
        n = n - 5;
 
    if ( n == 0 )
        return true;
 
    return false;
}
 
// Driver Code
int main()
{
    int n = 19;
    if ( isMultipleof5(n) == true )
        cout << n <<" is multiple of 5";
    else
        cout << n << " is not a multiple of 5";
 
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10


C




#include<stdio.h>
 
// assumes that n is a positive integer
bool isMultipleof5 (int n)
{
    while ( n > 0 )
        n = n - 5;
 
    if ( n == 0 )
        return true;
 
    return false;
}
 
// Driver Code
int main()
{
    int n = 19;
    if ( isMultipleof5(n) == true )
        printf("%d is multiple of 5\n", n);
    else
        printf("%d is not a multiple of 5\n", n);
 
    return 0;
}


Java




// Java code to check if a number
// is multiple of 5 without using
// '/' and '%' operators
class GFG
{
     
// assumes that n is a positive integer
static boolean isMultipleof5 (int n)
{
    while (n > 0)
        n = n - 5;
 
    if (n == 0)
        return true;
 
    return false;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 19;
    if (isMultipleof5(n) == true)
        System.out.printf("%d is multiple of 5\n", n);
    else
        System.out.printf("%d is not a multiple of 5\n", n);
}
}
 
// This code is contributed by Smitha DInesh Semwal


Python3




# Python code to check
# if a number is multiple of
# 5 without using / and %
 
# Assumes that n is a positive integer
def isMultipleof5(n):
     
    while ( n > 0 ):
        n = n - 5
 
    if ( n == 0 ):
        return 1
 
    return 0
     
# Driver Code
i = 19
if ( isMultipleof5(i) == 1 ):
    print (i, "is multiple of 5")
else:
    print (i, "is not a multiple of 5")
 
# This code is contributed
# by Sumit Sudhakar


C#




// C# code to check if a number
// is multiple of 5 without using /
// and % operators
using System;
 
class GFG
{
     
// assumes that n is a positive integer
static bool isMultipleof5 (int n)
{
    while (n > 0)
        n = n - 5;
 
    if (n == 0)
        return true;
 
    return false;
}
 
// Driver Code
public static void Main()
{
    int n = 19;
    if (isMultipleof5(n) == true)
        Console.Write(n + " is multiple of 5\n");
    else
        Console.Write(n + " is not a multiple of 5\n");
}
}
 
// This code is contributed by nitin mittal.


PHP




<?php
// assumes that n is a positive integer
function isMultipleof5 ($n)
{
    while ( $n > 0 )
        $n = $n - 5;
 
    if ( $n == 0 )
        return true;
 
    return false;
}
 
// Driver Code
    $n = 19;
    if ( isMultipleof5($n) == true )
        echo("$n is multiple of 5");
    else
        echo("$n is not a multiple of 5" );
         
// This code is contributed by nitin mittal.
?>


Javascript




<script>
 
// JavaScript program for the above approach
 
// assumes that n is a positive integer
function isMultipleof5 (n)
{
    while (n > 0)
        n = n - 5;
 
    if (n == 0)
        return true;
 
    return false;
}
 
// Driver Code
    let n = 19;
    if (isMultipleof5(n) == true)
        document.write(n + " is multiple of 5\n");
    else
        document.write(n + " is not a multiple of 5\n");
 
</script>


Output

19 is not a multiple of 5

Time Complexity: O(n / 5)
Auxiliary Space: O(1)

Method 2 (Convert to string and check the last character) 
Convert n to a string and check the last character of the string. If the last character is ‘5’ or ‘0’ then n is a multiple of 5, otherwise not. 

Below is the implementation of the above approach:

C++




#include <bits/stdc++.h>
using namespace std;
 
// Assuming that integer takes 4 bytes, there
// can be maximum 10 digits in a integer
#define MAX 20
 
bool isMultipleof5(int n)
{
    // in-built method to convert integer to string
    string str = to_string(n);
    int len = str.size();
 
    // Check the last character of string
    if (str[len - 1] == '5' || str[len - 1] == '0')
 
        return true;
 
    return false;
}
 
// Driver Code
int main()
{
    int n = 19;
    if (isMultipleof5(n) == true)
        cout << n << " is multiple of 5" << endl;
    else
        cout << n << " is not multiple of 5" << endl;
 
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10


C




#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <stdbool.h>
 
// Assuming that integer takes 4 bytes, there
// can be maximum 10 digits in a integer
# define MAX 11
 
bool isMultipleof5(int n)
{
    char str[MAX];
    // in-built method to convert integer to string
    sprintf(str, "%d", n);
    int len = strlen(str);
    printf("%d\n", len);
     
    // Check the last character of string
    if ( str[len - 1] == '5' ||
        str[len - 1] == '0' )
         
        return true;
     
    return false;
}
 
// Driver Code
int main()
{
    int n = 19;
    if ( isMultipleof5(n) == true )
        printf("%d is multiple of 5\n", n);
    else
        printf("%d is not a multiple of 5\n", n);
 
    return 0;
}


Java




// Assuming that integer
// takes 4 bytes, there
// can be maximum 10
// digits in a integer
class GFG
{
static int MAX = 11;
 
static boolean isMultipleof5(int n)
{
    // in-built method to convert integer to string
    String str = Integer.toString(n);
    int len = str.length();
     
    // Check the last
    // character of string
    if (str.charAt(len - 1) == '5' ||
        str.charAt(len - 1) == '0' )
         
        return true;
     
    return false;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 19;
    if ( isMultipleof5(n) == true )
        System.out.println(n +" is multiple " +
                                       "of 5");
    else
        System.out.println(n +" is not a " +
                           "multiple of 5");
}
}
 
// This code is contributed by mits


Python3




# Assuming that integer
# takes 4 bytes, there
# can be maximum 10
# digits in a integer
MAX = 11;
 
def isMultipleof5(n):
    # in-built method to convert integer to string
    s = str(n);
    l = len(s);
     
    # Check the last
    # character of string
    if (s[l - 1] == '5' or
        s[l - 1] == '0'):
        return True;
    return False;
 
# Driver Code
n = 19;
if (isMultipleof5(n) == True ):
    print(n, "is multiple of 5");
else:
    print(n, "is not a multiple of 5");
 
# This code is contributed by mits


C#




using System;
 
// Assuming that integer
// takes 4 bytes, there
// can be maximum 10
// digits in a integer
class GFG
{
 
static bool isMultipleof5(int n)
{
    // in-built method to convert integer to string
    String str = n.ToString();
    int len = str.Length;
     
    // Check the last
    // character of string
    if (str[len - 1] == '5' ||
        str[len - 1] == '0' )
         
        return true;
     
    return false;
}
 
// Driver Code
static void Main()
{
    int n = 19;
    if ( isMultipleof5(n) == true )
        Console.WriteLine("{0} is " +
                          "multiple of 5", n);
    else
        Console.WriteLine("{0} is not a " +
                          "multiple of 5", n);
}
}
 
// This code is contributed by mits


PHP




<?php
// Assuming that integer
// takes 4 bytes, there
// can be maximum 10
// digits in a integer
$MAX = 11;
 
function isMultipleof5($n)
{
    global $MAX;
    $str = (string)$n;
    $len = strlen($str);
     
    // Check the last
    // character of string
    if ($str[$len - 1] == '5' ||
        $str[$len - 1] == '0')
         
        return true;
     
    return false;
}
 
// Driver Code
$n = 19;
if (isMultipleof5($n) == true )
    echo "$n is multiple of 5";
else
    echo "$n is not a multiple of 5";
 
// This code is contributed by mits
?>


Javascript




<script>
 
// Assuming that integer
// takes 4 bytes, there
// can be maximum 10
// digits in a integer
MAX = 11;
 
function isMultipleof5(n)
{
    str = Array(n).fill('');
    var len = str.length;
     
    // Check the last
    // character of string
    if (str[len - 1] == '5' ||
        str[len - 1] == '0'
        return true;
     
    return false;
}
 
// Driver Code
var n = 19;
if ( isMultipleof5(n) == true )
    document.write(n +" is multiple " +
                                   "of 5");
else
    document.write(n +" is not a " +
                       "multiple of 5");
                        
// This code is contributed by Amit Katiyar
</script>


Output

19 is not multiple of 5

Time Complexity: O(1)
Auxiliary Space: O(MAX)

Thanks to Baban_Rathore for suggesting this method.
Method 3 (Set last digit as 0 and use floating point trick) 
A number n can be a multiple of 5 in two cases. When last digit of n is 5 or 10. If last bit in binary equivalent of n is set (which can be the case when last digit is 5) then we multiply by 2 using n<<=1 to make sure that if the number is multiple of 5 then we have the last digit as 0. Once we do that, our work is to just check if the last digit is 0 or not, which we can do using float and integer comparison trick. 

Below is the implementation of the above approach:

C++




#include <iostream>
using namespace std;
bool isMultipleof5(int n)
{
    // If n is a multiple of 5 then we
    // make sure that last digit of n is 0
    if ( (n & 1) == 1 )
        n <<= 1;
     
    float x = n;
    x = ( (int)(x * 0.1) ) * 10;
     
    // If last digit of n is 0 then n
    // will be equal to (int)x
    if ( (int)x == n )
        return true;
 
    return false;
}
 
// Driver Code
int main()
{
    int i = 19;
    if ( isMultipleof5(i) == true )
        cout << i <<" is multiple of 5\n";
    else
        cout << i << " is not a multiple of 5\n";
 
    getchar();
    return 0;
}
 
// This code is contributed by shubhamsingh10


C




#include<stdio.h>
 
bool isMultipleof5(int n)
{
    // If n is a multiple of 5 then we
    // make sure that last digit of n is 0
    if ( (n & 1) == 1 )
        n <<= 1;
     
    float x = n;
    x = ( (int)(x * 0.1) ) * 10;
     
    // If last digit of n is 0 then n
    // will be equal to (int)x
    if ( (int)x == n )
        return true;
 
    return false;
}
 
// Driver Code
int main()
{
    int i = 19;
    if ( isMultipleof5(i) == true )
        printf("%d is multiple of 5\n", i);
    else
        printf("%d is not a multiple of 5\n", i);
 
    getchar();
    return 0;
}


Java




// Java code to check if
// a number is multiple of
// 5 without using / and %
class GFG
{
static boolean isMultipleof5(int n)
{
    // If n is a multiple of 5
    // then we make sure that
    // last digit of n is 0
    if ((n & 1) == 1)
        n <<= 1;
     
    float x = n;
    x = ((int)(x * 0.1)) * 10;
     
    // If last digit of n is
    // 0 then n will be equal
    // to (int)x
    if ((int)x == n)
        return true;
 
    return false;
}
 
// Driver Code
public static void main(String[] args)
{
    int i = 19;
    if (isMultipleof5(i) == true)
        System.out.println(i + "is multiple of 5");
    else
        System.out.println(i + " is not a " +
                            "multiple of 5");
}
}
 
// This code is contributed
// by mits


Python3




# Python code to check
# if a number is multiple of
# 5 without using / and %
 
def isMultipleof5(n):
     
    # If n is a multiple of 5 then we
    # make sure that last digit of n is 0
    if ( (n & 1) == 1 ):
        n <<= 1;
 
    x = n
    x = ( (int)(x * 0.1) ) * 10
     
    # If last digit of n is 0
    # then n will be equal to x
    if ( x == n ):
        return 1
 
    return 0
     
# Driver Code
i = 19
if ( isMultipleof5(i) == 1 ):
    print (i, "is multiple of 5")
else:
    print (i, "is not a multiple of 5")
 
# This code is contributed
# by Sumit Sudhakar


C#




// C# code to check if
// a number is multiple of
// 5 without using / and %
class GFG
{
static bool isMultipleof5(int n)
{
    // If n is a multiple of 5
    // then we make sure that
    // last digit of n is 0
    if ((n & 1) == 1)
        n <<= 1;
     
    float x = n;
    x = ((int)(x * 0.1)) * 10;
     
    // If last digit of n is
    // 0 then n will be equal
    // to (int)x
    if ((int)x == n)
        return true;
 
    return false;
}
 
// Driver Code
public static void Main()
{
    int i = 19;
    if (isMultipleof5(i) == true)
        System.Console.WriteLine(i + "is multiple of 5");
    else
        System.Console.WriteLine(i + " is not a " +
                                   "multiple of 5");
}
}
 
// This code is contributed
// by mits


PHP




<?php
function isMultipleof5($n)
{
    // If n is a multiple of 5
    // then we make sure that
    // last digit of n is 0
    if (($n & 1) == 1 )
        $n <<= 1;
     
    $x = $n;
    $x = ((int)($x * 0.1)) * 10;
     
    // If last digit of n
    // is 0 then n will be
    // equal to (int)x
    if ( (int)($x) == $n )
        return true;
 
    return false;
}
 
// Driver Code
$i = 19;
if ( isMultipleof5($i) == true )
    echo "$i is multiple of 5\n";
else
    echo "$i is not a multiple of 5\n";
 
// This code is contributed by mits
?>


Javascript




<script>
// JavaScript code to check if
// a number is multiple of
// 5 without using / and %
function isMultipleof5( n)
{    
 
    // If n is a multiple of 5 then we
    // make sure that last digit of n is 0
    if ( (n & 1) == 1 )
        n <<= 1;
      
    x = ( (int)(x * 0.1) ) * 10;
     
    // If last digit of n is 0 then n
    // will be equal to (int)x
    if ( (int)(x == n))
        return true;
  
    return false;
}
  
// Driver Code
      i = 19;
    if ( isMultipleof5 == true)
        document.write( i + " is multiple of 5\n");
    else
        document.write( i + " is not a multiple of 5\n");
         
      // This code is contributed by simranarora5sos
    </script>


Output

19 is not a multiple of 5

Time Complexity: O(1)
Auxiliary Space: O(1)

Thanks to darkprince for suggesting this method.
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!