Check if a large number is divisible by 5 or not - GeeksforGeeks

28 Jan, 2022
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 : No
Since input number may be very large, we cannot use n % 5 to check if a number is divisible by 5 or not, especially in languages like C/C++. The idea is based on following fact. 
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
Illustration:  
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
How does this work? 
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
... etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
Below is the implementation of above idea.
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find if a number is// divisible by 5 or not#include<bits/stdc++.h>using namespace std; // Function to find that number divisible// by 5 or not. The function assumes that// string length is at least one.bool isDivisibleBy5(string str){    int n = str.length();     return ( ((str[n-1]-'0') == 0) ||             ((str[n-1]-'0') == 5));} // Driver codeint main(){    string str = "76955";    isDivisibleBy5(str)?  cout << "Yes" :                          cout << "No ";    return 0;}
// Java program to find if a number is// divisible by 5 or notclass IsDivisible{    // Function to find that number divisible    // by 5 or not. The function assumes that    // string length is at least one.    static boolean isDivisibleBy5(String str)    {        int n = str.length();              return ( ((str.charAt(n-1)-'0') == 0) ||                 ((str.charAt(n-1)-'0') == 5));    }         // main function    public static void main (String[] args)    {        String str = "76955";        if(isDivisibleBy5(str))            System.out.println("Yes");        else            System.out.println("No");    }}
# Python program to find if a number is# divisible by 5 or not # Function to find that number divisible# by 5 or not. The function assumes that# string length is at least one.def isDivisibleBy5(st) :    n = len(st)    return ( (st[n-1] == '0') or           (st[n-1] == '5'))  # Driver codest = "76955"if isDivisibleBy5(st) :    print ("Yes")else :    print ("No")     # This code is contributed by Nikita Tiwari.
// C# program to find if a number is// C# program to find if a number// is divisible by 5 or not.using System; class IsDivisible{    // Function to find that number divisible    // by 5 or not. The function assumes that    // string length is at least one.    static bool isDivisibleBy5(String str)    {        int n = str.Length;             return (((str[n - 1] - '0') == 0) ||                ((str[n - 1] - '0') == 5));    }         // Driver Code    public static void Main ()    {        String str = "76955";        if(isDivisibleBy5(str))        Console.Write("Yes");        else        Console.Write("No");    }} // This code is contributed by Nitin Mittal.
<?php// PHP program to find if a number// is divisible by 5 or not // Function to find that number divisible// by 5 or not. The function assumes that// string length is at least one.function isDivisibleBy5($str){    $n = strlen($str);     return ( (($str[$n-1]-'0') == 0) ||            (($str[$n-1]-'0') == 5));} // Driver code$str = "76955";$x = isDivisibleBy5($str) ? "Yes" : "No";echo($x); // This code is contributed by Ajit.?>
<script> // Javascript program to find if a number// is divisible by 5 or not   // Function to find that number divisible// by 5 or not. The function assumes that// string length is at least one.function  isDivisibleBy5(str){    n = str.length;       return (((str[n - 1] - '0') == 0) ||            ((str[n - 1] - '0') == 5));}   // Driver Codevar str = "76955";var x = isDivisibleBy5(str) ? "Yes" : "No"; document.write(x); // This code is contributed by bunnyram19 </script>
Output: 
Yes
This article is contributed by DANISH_RAZA . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
jit_t
ManasChhabra2
bunnyram19
surinderdawra388
amartyaghoshgfg
divisibility
large-numbers
Modular Arithmetic
Mathematical
Mathematical
Modular Arithmetic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Modular multiplicative inverse
Program to print prime numbers from 1 to N.
Program to multiply two matrices
Count ways to reach the n'th stair
Fizz Buzz Implementation
Program to convert a given number to words
Check if a number is Palindrome
Find first and last digits of a number
Generate all permutation of a set in Python