Number is divisible by 29 or not
Last Updated :
22 Feb, 2023
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 if 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++
#include <iostream>
using namespace std;
bool isDivisible( long long int n)
{
while (n / 100)
{
int last_digit = n % 10;
n /= 10;
n += last_digit * 3;
}
return (n % 29 == 0);
}
int main()
{
long long int n = 348;
if (isDivisible(n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isDivisible( long n)
{
while (n / 100 > 0 ) {
int last_digit = ( int )n % 10 ;
n /= 10 ;
n += last_digit * 3 ;
}
return (n % 29 == 0 );
}
public static void main(String[] args)
{
long n = 348 ;
if (isDivisible(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isDivisible(n):
while ( int (n / 100 )) :
last_digit = int (n % 10 )
n = int (n / 10 )
n + = last_digit * 3
return (n % 29 = = 0 )
n = 348
if (isDivisible(n) ! = 0 ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isDivisible( long n)
{
while (n / 100 > 0)
{
int last_digit = ( int )n % 10;
n /= 10;
n += last_digit * 3;
}
return (n % 29 == 0);
}
public static void Main()
{
long n = 348;
if (isDivisible(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function isDivisible( $n )
{
while ( intval ( $n / 100))
{
$last_digit = $n % 10;
$n = intval ( $n / 10);
$n += $last_digit * 3;
}
return ( $n % 29 == 0);
}
$n = 348;
if (isDivisible( $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isDivisible(n)
{
while (parseInt(n / 100))
{
let last_digit = n % 10;
n = parseInt(n / 10);
n += last_digit * 3;
}
return (n % 29 == 0);
}
let n = 348;
if (isDivisible(n))
document.write( "Yes" );
else
document.write( "No" ) ;
</script>
|
Time Complexity: O(n) where n is given number.
Space Complexity: O(1) as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...