Given a number s in the form of string, the task is to check if the number is divisible by 5.
Examples:
Input : s = 56945255
Output : trueInput : s = 12345
Output : trueInput : s = 36358839596
Output : false
Try It Yourself
Table of Content
[Naive Approach] String to Integer Conversion - O(n) Time and O(1) Space
Convert the given string to an integer by iterating through each digit, then check if it's divisible by 5 using modulo operator. This approach might cause overflow for very large numbers.
- Initialize num = 0.
- Traverse each character in string.
- Update num = num × 10 + (digit).
- Return true if num % 5 == 0 else false.
#include <bits/stdc++.h>
using namespace std;
bool divisibleBy5(string &s)
{
long long num = 0;
// Convert the string into a number
for (char ch : s)
{
num = num * 10 + (ch - '0');
}
// Check divisibility by 5
return (num % 5 == 0);
}
int main()
{
string s = "125";
cout << divisibleBy5(s);
return 0;
}
import java.util.*;
class GFG {
static boolean divisibleBy5(String s) {
long num = 0;
// Convert the string into a number
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
num = num * 10 + (ch - '0');
}
// Check divisibility by 5
return (num % 5 == 0);
}
public static void main(String[] args) {
String s = "125";
System.out.print(divisibleBy5(s));
}
}
def divisibleBy5(s):
num = 0
# Convert the string into a number
for ch in s:
num = num * 10 + (ord(ch) - ord('0'))
# Check divisibility by 5
return (num % 5 == 0)
if __name__ == "__main__":
s = "125"
print(divisibleBy5(s))
using System;
class GFG {
static bool divisibleBy5(string s) {
long num = 0;
// Convert the string into a number
foreach (char ch in s) {
num = num * 10 + (ch - '0');
}
// Check divisibility by 5
return (num % 5 == 0);
}
static void Main(string[] args) {
string s = "125";
Console.Write(divisibleBy5(s));
}
}
function divisibleBy5(s) {
let num = 0;
// Convert the string into a number
for (let i = 0; i < s.length; i++) {
let ch = s[i];
num = num * 10 + (ch.charCodeAt(0) - '0'.charCodeAt(0));
}
// Check divisibility by 5
return (num % 5 === 0);
}
// Driver code
let s = "125";
console.log(divisibleBy5(s));
Output
1
[Optimal Approach] Last Digit Check - O(1) Time and O(1) Space
A number is divisible by 5 if and only if its last digit is 0 or 5. Since the number can be very large (given as string), directly check the last character instead of converting to integer.
- Get last character of string using s.back().
- Return 1 if last digit is '0' or '5'.
- Return 0 otherwise.
#include <bits/stdc++.h>
using namespace std;
int divisibleBy5(string &s)
{
// Get the last digit of the number
char lastDigit = s.back();
// A number is divisible by 5
// if its last digit is 0 or 5
if (lastDigit == '0' || lastDigit == '5')
{
return 1;
}
return 0;
}
int main()
{
string s = "125";
cout << divisibleBy5(s);
return 0;
}
import java.util.*;
class GFG {
static boolean divisibleBy5(String s) {
// Get the last digit of the number
char lastDigit = s.charAt(s.length() - 1);
// A number is divisible by 5
// if its last digit is 0 or 5
if (lastDigit == '0' || lastDigit == '5') {
return true;
}
return false;
}
public static void main(String[] args) {
String s = "125";
System.out.print(divisibleBy5(s));
}
}
def divisibleBy5(s):
# Get the last digit of the number
lastDigit = s[-1]
# A number is divisible by 5
# if its last digit is 0 or 5
if lastDigit == '0' or lastDigit == '5':
return True
return False
if __name__ == "__main__":
s = "125"
print(divisibleBy5(s))
using System;
class GFG {
static bool divisibleBy5(string s) {
// Get the last digit of the number
char lastDigit = s[s.Length - 1];
// A number is divisible by 5
// if its last digit is 0 or 5
if (lastDigit == '0' || lastDigit == '5') {
return true;
}
return false;
}
static void Main(string[] args) {
string s = "125";
Console.Write(divisibleBy5(s));
}
}
function divisibleBy5(s) {
// Get the last digit of the number
let lastDigit = s[s.length - 1];
// A number is divisible by 5
// if its last digit is 0 or 5
if (lastDigit === '0' || lastDigit === '5') {
return true;
}
return false;
}
// Driver code
let s = "125";
console.log(divisibleBy5(s));
Output
1