Professional Documents
Culture Documents
Check Divisibility by 7
Check Divisibility by 7
Check divisibility by 7
Difficulty Level : Easy ● Last Updated : 17 Jun, 2019
interesting method.
In other words, subtract twice the last digit from the number
number.
C++
WHAT’S NEW
Ad-Free Experience –
GeeksforGeeks Premium
// A Program to check whether a number is divisible by 7 View Details
#include <bits/stdc++.h>
using namespace std;
// Base cases
if( num == 0 || num == 7 )
return 1;
if( num < 10 )
return 0;
// Driver code
int main()
{
int num = 616;
if( isDivisibleBy7(num ) )
cout << "Divisible" ;
else
cout << "Not Divisible" ;
return 0;
}
C
MOST POPULAR IN
MATHEMATICAL
// A Program to check whether a number is divisible by 7
#include <stdio.h>
Counting Sort
int isDivisibleBy7( int num )
{ Program for Decimal to Binary
// If number is negative, make it positive
Conversion
if( num < 0 )
return isDivisibleBy7( -num );
// Base cases
Program to find GCD or HCF of two
if( num == 0 || num == 7 ) numbers
return 1;
if( num < 10 )
return 0;
Merge two sorted arrays
Java
// Base cases
if( num == 0 || num == 7 )
return true;
if( num < 10 )
return false;
P ython
# Base cases
if( num == 0 or num == 7 ) :
return True
# Driver program
num = 616
if(isDivisibleBy7(num)) :
print "Divisible"
else :
print "Not Divisible"
C#
class GFG {
// If number is negative,
// make it positive
if( num < 0 )
return isDivisibleBy7(-num);
// Base cases
if( num == 0 || num == 7 )
return true;
if( num < 10 )
return false;
// Driver Code
public static void Main ()
{
int num = 616;
if(isDivisibleBy7(num))
Console.Write("Divisible");
else
Console.Write("Not Divisible");
}
}
PHP
<?php
// PHP Program to check whether
// a number is divisible by 7
// If number is negative,
// make it positive
if( $num < 0 )
return isDivisibleBy7( -$num );
// Base cases
if( $num == 0 || $num == 7 )
return 1;
if( $num < 10 )
return 0;
// Driver Code
$num = 616;
if( isDivisibleBy7($num )>=0 )
echo("Divisible");
else
echo("Not Divisible");
Output :
Divisible
How does this work? Let ‘b’ be the last digit of a number ‘n’ and let
divisibilit y. Af ter obser ving that 7 divides 21, we can per form the
following :
10.a + b
20.a + 2.b
and then
21.a - a + 2.b
-a + 2b
a - 2b
References :
http://en.wikipedia.org/wiki/Divisibilit y_rule
Attention reader! Don’t stop learning now. Get hold of all the
Like 0
Previous Next
To check whether a Program to print all the
large number is numbers divisible by 3
divisible by 7 and 5 for a given
number
To check divisibility
02 of any large number Sub-string Divisibility
by 999 06 by 11 Queries
26, Jun 17 08, Aug 16
Check if a large
03
number is divisibility Divisibility by 12 for a
07
by 15 large number
02, Nov 17 23, Jan 18
Ar ticle Contributed By :
GeeksforGeeks
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments