Gapful Numbers
Last Updated :
24 Mar, 2021
Gapful Number is a number N of at least 3 digits such that it is divisible by the concatenation of it’s first and last digit.
Few Gapful Numbers are:
100, 105, 108, 110, 120, 121, 130, 132, 135, 140,…
Check if N is a Gapful Number
Given an integer N, the task is to check whether N is a Gapful Number or not. If N is a Gapful Number then print “Yes” else print “No”.
Examples:
Input: N = 108
Output: Yes
Explanation:
108 is divisible by 18
Input: N = 112
Output: No
Approach: The idea is to create a number(say num) using the first and last digits of the given numbers and check whether N is divisible by num or not. If N is divisible by num then it is a Gapful Number and print “Yes”, else print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int firstDigit( int n)
{
int digits = ( int ) log10 (n);
n = ( int )(n / pow (10, digits));
return n;
}
int lastDigit( int n)
{
return (n % 10);
}
bool isGapful( int n)
{
int first_dig = firstDigit(n);
int last_dig = lastDigit(n);
int concatenation = first_dig * 10
+ last_dig;
return (n % concatenation == 0);
}
int main()
{
int n = 108;
if (isGapful(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static int firstDigit( int n)
{
int digits = ( int )(Math.log(n) /
Math.log( 10 ));
n = ( int )(n / Math.pow( 10 , digits));
return n;
}
static int lastDigit( int n)
{
return (n % 10 );
}
static boolean isGapful( int n)
{
int first_dig = firstDigit(n);
int last_dig = lastDigit(n);
int concatenation = first_dig * 10 +
last_dig;
return (n % concatenation == 0 );
}
public static void main(String[] args)
{
int n = 108 ;
if (isGapful(n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
import math
def firstDigit(n):
digits = math.log10(n)
n = (n / math. pow ( 10 , digits))
return n
def lastDigit(n):
return (n % 10 )
def isGapful(n):
concatenation = (firstDigit(n) * 10 ) + \
lastDigit(n)
return (n % concatenation)
if __name__ = = '__main__' :
n = 108
if (isGapful(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int firstDigit( int n)
{
int digits = ( int )(Math.Log(n) /
Math.Log(10));
n = ( int )(n / Math.Pow(10, digits));
return n;
}
static int lastDigit( int n)
{
return (n % 10);
}
static bool isGapful( int n)
{
int first_dig = firstDigit(n);
int last_dig = lastDigit(n);
int concatenation = first_dig * 10 +
last_dig;
return (n % concatenation == 0);
}
public static void Main()
{
int n = 108;
if (isGapful(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function firstDigit( n)
{
let digits = parseInt( (Math.log(n) / Math.log(10)));
n = parseInt( (n / Math.pow(10, digits)));
return n;
}
function lastDigit( n)
{
return (n % 10);
}
function isGapful( n)
{
let first_dig = firstDigit(n);
let last_dig = lastDigit(n);
let concatenation = first_dig * 10 + last_dig;
return (n % concatenation == 0);
}
let n = 108;
if (isGapful(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Reference: https://oeis.org/A108343
Share your thoughts in the comments
Please Login to comment...