Check if a large number is divisible by 2, 3 and 5 or not
Last Updated :
31 Mar, 2023
Given a number, the task is to check if a number is divisible by 2, 3, and 5 or not. The input number may be large and it may not be possible to store even if we use long long int, so the number is taken as a string.
Examples:
Input : str = "725"
Output : NO
Input : str = "263730746028908374890"
Output : YES
A number is divisible by 2 if it's right most digit is even and also a number is divisible by 5 if it's right most digit is zero or five.
So, from above two observations, one can conclude that for the number to be divisible by both 2 and 5 the rightmost digit of the number must be zero.
Now, a number is divisible by 3 if the sum of its digits is divisible by three.
Therefore, a number will be divisible by all of 2, 3, and 5 if:
- Its rightmost digit is zero.
- Sum of all of its digits is divisible by 3.
Below is the implementation of the above approach:
C++
// CPP program to Check if a large number is
// divisible by 2, 3 and 5 or not.
#include <bits/stdc++.h>
using namespace std;
// function to return sum of digits of
// a number
int SumOfDigits(string str, int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(str[i] - '0');
return sum;
}
// function to Check if a large number is
// divisible by 2, 3 and 5 or not
bool Divisible(string str, int n)
{
if (SumOfDigits(str, n) % 3 == 0 and str[n - 1] == '0')
return true;
return false;
}
// Driver code
int main()
{
string str = "263730746028908374890";
int n = str.size();
if (Divisible(str, n))
cout << "YES";
else
cout << "NO";
return 0;
}
C
// C program to Check if a large number is
// divisible by 2, 3 and 5 or not.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
// function to return sum of digits of
// a number
int SumOfDigits(char str[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(str[i] - '0');
return sum;
}
// function to Check if a large number is
// divisible by 2, 3 and 5 or not
bool Divisible(char str[], int n)
{
if (SumOfDigits(str, n) % 3 == 0 && str[n - 1] == '0')
return true;
return false;
}
// Driver code
int main()
{
char str[] = "263730746028908374890";
int n = strlen(str);
if (Divisible(str, n))
printf("YES");
else
printf("NO");
return 0;
}
// This code is contributed by kothavvsaakash.
Java
// Java program to Check if a large
// number is divisible by 2, 3 and
// 5 or not.
import java.io.*;
class GFG
{
// function to return sum of
// digits of a number
static int SumOfDigits(String str,
int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(str.charAt(i) - '0');
return sum;
}
// function to Check if a large number
// is divisible by 2, 3 and 5 or not
static boolean Divisible(String str,
int n)
{
if (SumOfDigits(str, n) % 3 == 0 &&
str.charAt(n - 1) == '0')
return true;
return false;
}
// Driver code
public static void main(String []args)
{
String str = "263730746028908374890";
int n = str.length();
if (Divisible(str, n))
System.out.println("YES");
else
System.out.println("NO");
}
}
// This code is contributed by ihritik
Python 3
# Python 3 program to Check if
# a large number is
# divisible by 2, 3 and 5 or not.
# function to return sum of digits of
# a number
def SumOfDigits(str, n):
sum = 0
for i in range(0,n):
sum += int(ord(str[i] )- ord('0'))
return sum
# function to Check if a large number is
# divisible by 2, 3 and 5 or not
def Divisible(str, n):
if ((SumOfDigits(str, n) % 3 == 0 and
str[n - 1] == '0')):
return True
return False
# Driver code
if __name__ == "__main__":
str = "263730746028908374890"
n = len(str)
if (Divisible(str, n)):
print("YES")
else:
print("NO")
# this code is contributed by
# ChitraNayal
C#
// C# program to Check if a large number
// is divisible by 2, 3 and 5 or not.
using System;
class GFG
{
// function to return sum of digits
// of a number
static int SumOfDigits(String str,
int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(str[i] - '0');
return sum;
}
// function to Check if a large number
// is divisible by 2, 3 and 5 or not
static bool Divisible(String str, int n)
{
if (SumOfDigits(str, n) % 3 == 0 &&
str[n - 1] == '0')
return true;
return false;
}
// Driver code
public static void Main()
{
String str = "263730746028908374890";
int n = str.Length;
if (Divisible(str, n))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
// This code is contributed by ihritik
PHP
<?php
// PHP program to Check if a large number
// is divisible by 2, 3 and 5 or not.
// function to return sum of digits
// of a number
function SumOfDigits($str, $n)
{
$sum = 0;
for ($i = 0; $i < $n; $i++)
$sum += (int)($str[$i] - '0');
return $sum;
}
// function to Check if a large number
// is divisible by 2, 3 and 5 or not
function Divisible($str, $n)
{
if (SumOfDigits($str, $n) % 3 == 0 and
$str[$n - 1] == '0')
return true;
return false;
}
// Driver code
$str = "263730746028908374890";
$n = strlen($str);
if (Divisible($str, $n))
echo "YES";
else
echo "NO";
// This code is contributed
// by Shivi_Aggarwal
?>
JavaScript
<script>
// JavaScript program to Check if a large
// number is divisible by 2, 3 and
// 5 or not.
// Function to return sum of
// digits of a number
function SumOfDigits(str, n)
{
var sum = 0;
for(var i = 0; i < n; i++)
sum += (str.charAt(i) - '0');
return sum;
}
// Function to Check if a large number
// is divisible by 2, 3 and 5 or not
function Divisible(str, n)
{
if (SumOfDigits(str, n) % 3 == 0 &&
str.charAt(n - 1) == '0')
return true;
return false;
}
// Driver code
var str = "263730746028908374890";
var n = str.length;
if (Divisible(str, n))
document.write("YES");
else
document.write("NO");
// This code is contributed by Ankita saini
</script>
Time Complexity: O(n), where n is the size of the given string str
Auxiliary Space: O(1), as no extra space is required
Another approach:
Approach:
1. Check if the last digit of the number is even. If it is, then the number is divisible by 2.
2. Check if the sum of the digits of the number is divisible by 3. If it is, then the number is divisible by 3.
3. Check if the last digit of the number is 0 or 5. If it is, then the number is divisible by 5.
C++
#include <iostream>
using namespace std;
int main()
{
long long int n = 1234567890123456789; // example number
int sum = 0;
int last_digit = n % 10;
// Check for divisibility by 2
if (last_digit % 2 == 0) {
cout << n << " is divisible by 2\n";
}
else {
cout << n << " is not divisible by 2\n";
}
// Check for divisibility by 3
long long int temp_n = n;
while (temp_n > 0) {
sum += temp_n % 10;
temp_n /= 10;
}
if (sum % 3 == 0) {
cout << n << " is divisible by 3\n";
}
else {
cout << n << " is not divisible by 3\n";
}
// Check for divisibility by 5
if (last_digit == 0 || last_digit == 5) {
cout << n << " is divisible by 5\n";
}
else {
cout << n << " is not divisible by 5\n";
}
return 0;
}
C
#include <stdio.h>
int main() {
long long int n = 1234567890123456789; // example number
int sum = 0;
int last_digit = n % 10;
// Check for divisibility by 2
if (last_digit % 2 == 0) {
printf("%lld is divisible by 2\n", n);
} else {
printf("%lld is not divisible by 2\n", n);
}
// Check for divisibility by 3
while (n > 0) {
sum += n % 10;
n /= 10;
}
if (sum % 3 == 0) {
printf("%lld is divisible by 3\n", n);
} else {
printf("%lld is not divisible by 3\n", n);
}
// Check for divisibility by 5
if (last_digit == 0 || last_digit == 5) {
printf("%lld is divisible by 5\n", n);
} else {
printf("%lld is not divisible by 5\n", n);
}
return 0;
}
Java
// Java Equivalent
public class Main {
public static void main(String[] args) {
long n = 1234567890123456789L; // example number
int sum = 0;
int last_digit = (int) n % 10;
// Check for divisibility by 2
if (last_digit % 2 == 0) {
System.out.println(n + " is divisible by 2");
} else {
System.out.println(n + " is not divisible by 2");
}
// Check for divisibility by 3
long temp_n = n;
while (temp_n > 0) {
sum += temp_n % 10;
temp_n /= 10;
}
if (sum % 3 == 0) {
System.out.println(n + " is divisible by 3");
} else {
System.out.println(n + " is not divisible by 3");
}
// Check for divisibility by 5
if (last_digit == 0 || last_digit == 5) {
System.out.println(n + " is divisible by 5");
} else {
System.out.println(n + " is not divisible by 5");
}
}
}
Python3
n = 1234567890123456789 # example number
sum = 0
last_digit = n % 10
# Check for divisibility by 2
if last_digit % 2 == 0:
print(n, "is divisible by 2")
else:
print(n, "is not divisible by 2")
# Check for divisibility by 3
temp_n = n
while temp_n > 0:
sum += temp_n % 10
temp_n //= 10
if sum % 3 == 0:
print(n, "is divisible by 3")
else:
print(n, "is not divisible by 3")
# Check for divisibility by 5
if last_digit == 0 or last_digit == 5:
print(n, "is divisible by 5")
else:
print(n, "is not divisible by 5")
C#
using System;
class Gfg {
public static void Main() {
long n = 1234567890123456789; // example number
int sum = 0;
int last_digit = (int)(n % 10);
// Check for divisibility by 2
if (last_digit % 2 == 0) {
Console.WriteLine($"{n} is divisible by 2");
} else {
Console.WriteLine($"{n} is not divisible by 2");
}
// Check for divisibility by 3
long temp_n = n;
while (temp_n > 0) {
sum += (int)(temp_n % 10);
temp_n /= 10;
}
if (sum % 3 == 0) {
Console.WriteLine($"{n} is divisible by 3");
} else {
Console.WriteLine($"{n} is not divisible by 3");
}
// Check for divisibility by 5
if (last_digit == 0 || last_digit == 5) {
Console.WriteLine($"{n} is divisible by 5");
} else {
Console.WriteLine($"{n} is not divisible by 5");
}
}
}
JavaScript
let n = 1234567890123456789; // example number
// Check for divisibility by 2
if (n % 2 === 0) {
console.log(`${n} is divisible by 2`);
} else {
console.log(`${n} is not divisible by 2`);
}
// Check for divisibility by 3
let sum = 0;
let temp_n = n;
while (temp_n > 0) {
sum += temp_n % 10;
temp_n = Math.floor(temp_n / 10);
}
if (sum % 3 === 0) {
console.log(`${n} is divisible by 3`);
} else {
console.log(`${n} is not divisible by 3`);
}
// Check for divisibility by 5
let last_digit = n % 10;
if (last_digit === 0 || last_digit === 5) {
console.log(`${n} is divisible by 5`);
} else {
console.log(`${n} is not divisible by 5`);
}
Output1234567890123456789 is not divisible by 2
0 is divisible by 3
0 is not divisible by 5
Time Complexity: O(log10(n)), where n is the number being checked, because we iterate over each digit of the number once.
Space Complexity: O(1), because we only need a few variables to store the state of the algorithm
Similar Reads
Check if a large number is divisible by 25 or not
Given a number, the task is to check if number is divisible by 25. The input number may be large and it may not be possible to store even if we use long long int. Examples: Input : n = 56945250 Output : Yes Input : n = 1234567589333100 Output : Yes Input : n = 3635883959606670431112222 Output : No S
11 min read
Check if a large number is divisible by 5 or not
Given a number, the task is to check if number is divisible by 5. The input number may be large and it may not be possible to store even if we use long long int. Examples: Input : n = 56945255 Output : Yes Input : n = 1234567589333150 Output : Yes Input : n = 3635883959606670431112222 Output : NoRec
4 min read
Check if a large number is divisible by 3 or not
Given a number, the task is that we divide number by 3. The input number may be large and it may not be possible to store even if we use long long int.Examples: Input : n = 769452Output : YesInput : n = 123456758933312Output : NoInput : n = 3635883959606670431112222Output : YesSince input number may
7 min read
Check if a large number is divisible by 75 or not
Given a very large number in the form of a string, the task is to check if the number is divisible by 75 or not. Examples: Input: N = 175 Output: No Input: N = 100000000000000000754586672150 Output: Yes Approach 1: A number is divisible by 75 only if it is divisible by 3(if the sum of the digit is d
12 min read
Check if a large number is divisible by 13 or not
Given a large number, the task is to check if the number is divisible by 13 or not. Examples : Input : 637Output : 637 is divisible by 13.Input : 920Output : 920 is not divisible by 13.Input : 83959092724Output : 83959092724 is divisible by 13.[Naive Approach] - Modulo Division If the given number i
14 min read
Check if a large number is divisible by 6 or not
Given a number, the task is to check if a number is divisible by 6 or not. The input number may be large and it may not be possible to store even if we use long long int.Examples: Input : n = 2112Output: YesInput : n = 1124Output : NoInput : n = 363588395960667043875487Output : NoC++#include <ios
7 min read
Check if a large number is divisible by 4 or not
Given a number, the task is to check if a number is divisible by 4 or not. The input number may be large and it may not be possible to store even if we use long long int.Examples:Input : n = 1124Output : YesInput : n = 1234567589333862Output : NoInput : n = 363588395960667043875487Output : NoUsing t
12 min read
Check if a large number is divisible by 11 or not
Given a number, the task is to check if the number is divisible by 11 or not. The input number may be large and it may not be possible to store it even if we use long long int.Examples: Input : n = 76945Output : YesInput : n = 1234567589333892Output : YesInput : n = 363588395960667043875487Output :
7 min read
Check if a large number is divisible by 9 or not
Given a large number as a string s, determine if it is divisible by 9.Note: The number might be so large that it can't be stored in standard data types like long long.Examples: Input : s = "69354"Output: YesExplanation: 69354 is divisible by 9.Input: s = "234567876799333"Output: NoExplanation: 23456
3 min read
Check if a large number is divisible by 8 or not
Given a number, the task is to check if a number is divisible by 8 or not. The input number may be large and it may not be possible to store even if we use long long int.Examples: Input : n = 1128Output : YesInput : n = 1124Output : NoInput : n = 363588395960667043875487Output : NoSince input number
13 min read