Check if any large number is divisible by 19 or not - GeeksforGeeks

23 Mar, 2021
Given a number, the task is to quickly check if the number is divisible by 19 or not. Examples:  
Input : x = 38
Output : Yes

Input : x = 47
Output : No
A solution to the problem is to extract the last digit and add 2 times of last digit to remaining number and repeat this process until a two digit number is obtained. If the obtained two digit number is divisible by 19, then the given number is divisible by 19.Approach:  
Extract the last digit of the number/truncated number every time
Add 2*(last digit of the previous number) to the truncated number
Repeat the above three steps as long as necessary.
Illustration:  
101156-->10115+2*6 = 10127-->1012+2*7=1026-->102+2*6=114 and 114=6*19,
So 101156 is divisible by 19.
 
Mathematical Proof : Let be any number such that =100a+10b+c . Now assume that is divisible by 19. Then 0 (mod 19) 100a+10b+c0 (mod 19) 10(10a+b)+c0 (mod 19) 10+c0 (mod 19)Now that we have separated the last digit from the number, we have to find a way to use it. Make the coefficient of 1. In other words, we have to find an integer such that n such that 10n1 mod 19. It can be observed that the smallest n which satisfies this property is 2 as 201 mod 19. Now we can multiply the original equation 10+c0 (mod 19) by 2 and simplify it: 20+2c0 (mod 19) +2c0 (mod 19) We have found out that if 0 (mod 19) then, +2c0 (mod 19). In other words, to check if a 3-digit number is divisible by 19, we can just remove the last digit, multiply it by 2, and then add to the rest of the two digits.  
 
C++
Java
C#
PHP
Javascript
// CPP Program to validate the above logic#include <bits/stdc++.h>using namespace std; // Function to check if the number// is divisible by 19 or notbool isDivisible(long long int n){     while (n / 100) //    {        // Extracting the last digit        int d = n % 10;         // Truncating the number        n /= 10;         // Adding twice the last digit        // to the remaining number        n += d * 2;    }     // return true if number is divisible by 19    return (n % 19 == 0);} // Driver codeint main(){    long long int n = 101156;    if (isDivisible(n))        cout << "Yes" << endl;    else        cout << "No" << endl;    return 0;}
// Java Program to validate the above logicimport java.io.*; class GFG { // Function to check if the// number is divisible by 19 or notstatic boolean isDivisible(long n){     while (n / 100>0)    {        // Extracting the last digit        long d = n % 10;         // Truncating the number        n /= 10;         // Subtracting the five times the        // last digit from the remaining number        n += d * 2;    }     // Return n is divisible by 19    return (n % 19 == 0);} // Driver code     public static void main (String[] args) {    long n = 101156;    if (isDivisible(n))        System.out.println( "Yes");    else        System.out.println( "No");    }}// This code is contributed by Raj.
Python 3 

# Python 3 Program to check 
# if the number is divisible
# by 19 or not 

# Function to check if the number 
# is divisible by 19 or not 
def isDivisible(n) :
    
    while (n // 100) :
                
        # Extracting the last digit 
        d = n % 10

        # Truncating the number 
        n //= 10

        # Adding twice the last digit 
        # to the remaining number 
        n += d * 2

    # return true if number 
    # is divisible by 19 
    return (n % 19 == 0) 

# Driver Code
if __name__ == "__main__" :

    n = 101156
    
    if (isDivisible(n)) : 
        print("Yes" )
        
    else :
        print("No") 
    
# This code is contributed 
# by ANKITRAI1


// C# Program to validate the// above logicusing System; class GFG{     // Function to check if the// number is divisible by 19 or notstatic bool isDivisible(long n){     while (n / 100 > 0)    {        // Extracting the last digit        long d = n % 10;         // Truncating the number        n /= 10;         // Subtracting the five times        // the last digit from the        // remaining number        n += d * 2;    }     // Return n is divisible by 19    return (n % 19 == 0);} // Driver codepublic static void Main(){    long n = 101156;         if (isDivisible(n))        Console.WriteLine( "Yes");    else        Console.WriteLine( "No");}} // This code is contributed by ajit
<?php// PHP Program to validate// the above logic // Function to check if the number// is divisible by 19 or notfunction isDivisible( $n){         while (1)    {        // Extracting the last digit        $d = $n % 10;         // Truncating the number        $n = $n / 10;         // Adding twice the last digit        // to the remaining number        $n = $n + $d * 2;        if($n < 100)            break;    }         // return true if number is    // divisible by 19    return ($n % 19 == 0);} // Driver code$n = 38; if (isDivisible($n))    echo "Yes" ;else    echo "No" ; // This code is contributed by ash264?>
<script> // javascript Program to validate the above logic // Function to check if the// number is divisible by 19 or notfunction isDivisible(n){     while (parseInt(n / 100)>0)    {        // Extracting the last digit        var d = n % 10;         // Truncating the number        n = parseInt(n/ 10);         // Subtracting the five times the        // last digit from the remaining number        n += d * 2;    }     // Return n is divisible by 19    return (n % 19 == 0);} // Driver codevar n = 101156;if (isDivisible(n))    document.write( "Yes");else    document.write( "No"); // This code is contributed by 29AjayKumar </script>
Yes
 
Note that the above program may not make a lot of sense as could simply do n % 19 to check for divisibility. The idea of this program is to validate the concept. Also, this might be an efficient approach if input number is large and given as string. 
ankthon
ash264
R_Raj
Sach_Code
29AjayKumar
Articles
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Mutex vs Semaphore
Time Complexity and Space Complexity
SQL | Views
Understanding "extern" keyword in C
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Program to find GCD or HCF of two numbers