Check if a large number is divisible by 2, 3 and 5 or not
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; } |
Java
// Java program to Check if a large // number is divisible by 2, 3 and // 5 or not. 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 ?> |
YES
Recommended Posts:
- Check if any large number is divisible by 17 or not
- Check if a large number is divisible by 11 or not
- Check if any large number is divisible by 19 or not
- Check if a large number is divisible by 13 or not
- Check if a large number is divisible by 4 or not
- Check if a large number is divisible by 3 or not
- Check if a large number is divisible by 25 or not
- Check a large number is divisible by 16 or not
- To check whether a large number is divisible by 7
- Check if a large number is divisible by 75 or not
- Check if a large number is divisible by 5 or not
- Check if a large number is divisible by 20
- Check if a large number is divisible by 9 or not
- Check if a large number is divisible by 8 or not
- Check if a large number is divisible by 6 or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



