Check if the given number is divisible by 71 or not
Given a number N, the task is to check whether the number is divisible by 71 or not.
Examples:
Input: N = 25411681
Output: yes
Explanation:
71 * 357911 = 25411681
Input: N = 5041
Output: yes
Explanation:
71 * 71 = 5041
Approach: The divisibility test of 71 is:
- Extract the last digit.
- Subtract 7 * 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 71, or it is 0, then the original number is also divisible by 71.
For example:
If N = 5041
Step 1:
N = 5041
Last digit = 1
Remaining number = 504
Subtracting 7 times last digit
Resultant number = 504 - 7*1 = 497
Step 2:
N = 497
Last digit = 7
Remaining number = 49
Subtracting 7 times last digit
Resultant number = 49 - 7*7 = 0
Step 3:
N = 0
Since N is a two-digit number,
and 0 is divisible by 71
Therefore N = 5041 is also divisible by 71
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 * 7));
}
return (n % 71 == 0) ;
}
int main() {
int N = 5041;
if (isDivisible(N))
cout << "Yes" << endl ;
else
cout << "No" << endl ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isDivisible( int n)
{
int d;
while ((n / 100 ) <= 0 )
{
d = n % 10 ;
n /= 10 ;
n = Math.abs(n - (d * 7 ));
}
return (n % 71 == 0 ) ;
}
public static void main(String args[]){
int N = 5041 ;
if (isDivisible(N))
System.out.println( "Yes" ) ;
else
System.out.println( "No" );
}
}
|
Python 3
def isDivisible(n) :
while n / / 100 :
d = n % 10
n / / = 10
n = abs (n - (d * 7 ))
return (n % 71 = = 0 )
if __name__ = = "__main__" :
N = 5041
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 * 7));
}
return (n % 71 == 0);
}
public static void Main()
{
int N = 5041;
if (isDivisible(N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isDivisible(n)
{
let d;
while (Math.floor(n / 100) <=0)
{
d = n % 10;
n = Math.floor(n/10);
n = Math.abs(n - (d * 7));
}
return (n % 71 == 0) ;
}
let N = 5041;
if (isDivisible(N))
document.write( "Yes" ) ;
else
document.write( "No" );
</script>
|
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Last Updated :
07 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...