Check if the number is divisible 43 or not
Last Updated :
24 Nov, 2021
Given a number N, the task is to check whether the number is divisible by 43 or not.
Examples:
Input: N = 2795
Output: yes
Explanation:
43 * 65 = 2795
Input: N = 11094
Output: yes
Explanation:
43 * 258 = 11094
Approach: The divisibility test of 43 is:
- Extract the last digit.
- Add 13 * last digit from the remaining number obtained after removing the last digit.
- Repeat the above steps until a two-digit number, or zero, is obtained.
- If the two-digit number is divisible by 43, or it is 0, then the original number is also divisible by 43.
For example:
If N = 11739
Step 1:
N = 11739
Last digit = 9
Remaining number = 1173
Adding 13 times last digit
Resultant number = 1173 + 13*9 = 1290
Step 2:
N = 1290
Since 129 is divisible by 43 as 43 * 3 = 129
Therefore N = 11739 is also divisible by 43
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
#include<stdlib.h>
using namespace std;
bool isDivisible( int n)
{
int d;
while (n / 100)
{
d = n % 10;
n /= 10;
n = abs (n+(d * 13));
}
return (n % 43 == 0) ;
}
int main() {
int N = 2795;
if (isDivisible(N))
cout<< "Yes" <<endl ;
else
cout<< "No" <<endl ;
return 0;
}
|
Java
class GFG
{
static boolean isDivisible( int n)
{
int d;
while ((n / 100 ) > 0 )
{
d = n % 10 ;
n /= 10 ;
n = Math.abs(n+(d * 13 ));
}
return (n % 43 == 0 ) ;
}
public static void main(String[] args) {
int N = 2795 ;
if (isDivisible(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python 3
def isDivisible(n) :
while n / / 100 :
d = n % 10
n / / = 10
n = abs (n + (d * 13 ))
return (n % 43 = = 0 )
if __name__ = = "__main__" :
N = 2795
if (isDivisible(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isDivisible( int n)
{
int d;
while (n / 100 > 0)
{
d = n % 10;
n /= 10;
n = Math.Abs(n + (d * 13));
}
return (n % 43 == 0) ;
}
public static void Main()
{
int N = 2795;
if (isDivisible(N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isDivisible(n)
{
let d;
while (parseInt(n/100) > 0)
{
d = n % 10;
n = parseInt(n / 10)
n = Math.abs(n+(d * 13));
}
return (n % 43 == 0) ;
}
let N = 2795;
if (isDivisible(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...