Number is divisible by 29 or not

Given a large number n, find if the number is divisible by 29.

Examples :

Input : 363927598
Output : No

Input : 292929002929
Output : Yes

A quick solution to check if a number is divisible by 29 or not is to add 3 times of last digit to rest number and repeat this process until number comes 2 digit. The given number is divisible by 29 iff the obtained two digit number is divisible by 29.

Number is 348,
Three times of last digit + Rest of the number = 8*3 + 34 = 58

Since 58 is divisible by 29, 348 is also divisible by 29.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate above method
// to check divisibility by 29.
#include <iostream>
using namespace std;
  
// Returns true if n is divisible by 29
// else returns false.
bool isDivisible(long long int n)
{
    // add the lastdigit*3 to renaming 
    // number until number comes only
    // 2 digit
    while (n / 100) 
    {
        int last_digit = n % 10;
        n /= 10;
        n += last_digit * 3;
    }
  
    // return true if number is
    // divisible by 29 another
    return (n % 29 == 0);
}
  
// Driver Code
int main()
{
    long long int n = 348;
    if (isDivisible(n))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate above method
// to check divisibility by 29.
  
import java.io.*;
  
class GFG {
      
    // Returns true if n is divisible by 29
    // else returns false.
    static boolean isDivisible(long n)
    {
          
        // add the lastdigit*3 to renaming
        // number until number comes only
        // 2 digit
        while (n / 100 > 0) {
              
            int last_digit = (int)n % 10;
            n /= 10;
            n += last_digit * 3;
        }
  
        // return true if number is
        // divisible by 29 another
        return (n % 29 == 0);
    }
      
    // Driver code
    public static void main(String[] args)
    {
        long n = 348;
          
        if (isDivisible(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to demonstrate above 
# method to check divisibility by 29.
  
# Returns true if n is divisible 
# by 29 else returns false.
def isDivisible(n):
  
    # add the lastdigit*3 to renaming 
    # number until number comes only
    # 2 digit
    while (int(n / 100)) :
        last_digit = int(n % 10)
        n = int(n / 10)
        n += last_digit * 3
      
    # return true if number is
    # divisible by 29 another
    return (n % 29 == 0)
  
# Driver Code
n = 348
  
if(isDivisible(n) != 0):
    print("Yes")
else:
    print("No")
  
# This code is contributed by Smitha Dinesh Semwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate above method
// to check divisibility by 29.
using System;
class GFG 
{
      
    // Returns true if n is divisible by 29
    // else returns false.
    static bool isDivisible(long n)
    {
          
        // add the lastdigit*3 to renaming
        // number until number comes only
        // 2 digit
        while (n / 100 > 0) 
        {
              
            int last_digit = (int)n % 10;
            n /= 10;
            n += last_digit * 3;
        }
  
        // return true if number is
        // divisible by 29 another
        return (n % 29 == 0);
    }
      
    // Driver code
    public static void Main()
    {
        long n = 348;
          
        if (isDivisible(n))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
  
// This code is contributed by nitin mittal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate 
// above method to check 
// divisibility by 29.
  
// Returns true if n is 
// divisible by 29
// else returns false.
function isDivisible($n)
{
    // add the lastdigit*3 to 
    // remaining number until 
    // number becomes of only 
    // 2 digit
    while (intval($n / 100)) 
    {
        $last_digit = $n % 10;
        $n = intval($n / 10);
        $n += $last_digit * 3;
    }
  
    // return true if number is
    // divisible by 29 another
    return ($n % 29 == 0);
}
  
// Driver Code
$n = 348;
if (isDivisible($n))
    echo "Yes";
else
    echo "No" ;
  
// This code is contributed by Sam007
?>

chevron_right


Output :

Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : nitin mittal, Sam007



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.