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


Here we will see how to check a number is divisible by 5 or not. One simple approach is that if the number mod 5 = 0, then, the number is divisible by 5. But here we will not use / or % operator. To check whether a number is divisible by 5, we have to see the last number is 0 or 5. If that is 0 or 5, the number is divisible by 5, otherwise not. Here we can use some large numbers also as a string to check.

Example

#include <bits/stdc++.h>
using namespace std;
bool isDiv5(string num){
   int n = num.length();
   if(num[n - 1] != '5' && num[n - 1] != '0')
      return false;
   return true;
}
int main() {
   string num = "154484585745184258458158245285265";
   if(isDiv5(num)){
      cout << "Divisible";
   } else {
      cout << "Not Divisible";
   }
}

Output

Divisible

Updated on: 22-Oct-2019

588 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements