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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Find the first digit int firstDigit( int n) { // Find total number of digits - 1 int digits = ( int ) log10 (n); // Find first digit n = ( int )(n / pow (10, digits)); // Return first digit return n; } // Find the last digit int lastDigit( int n) { // return the last digit return (n % 10); } // A function to check Gapful numbers bool isGapful( int n) { int first_dig = firstDigit(n); int last_dig = lastDigit(n); int concatenation = first_dig * 10 + last_dig; // Return true if n is gapful number return (n % concatenation == 0); } // Driver Code int main() { // Given Number int n = 108; // Function Call if (isGapful(n)) cout << "Yes" ; else cout << "No" ; return 0; } |
Java
// Java program for the above approach class GFG{ // Find the first digit static int firstDigit( int n) { // Find total number of digits - 1 int digits = ( int )(Math.log(n) / Math.log( 10 )); // Find first digit n = ( int )(n / Math.pow( 10 , digits)); // Return first digit return n; } // Find the last digit static int lastDigit( int n) { // Return the last digit return (n % 10 ); } // A function to check Gapful numbers static boolean isGapful( int n) { int first_dig = firstDigit(n); int last_dig = lastDigit(n); int concatenation = first_dig * 10 + last_dig; // Return true if n is gapful number return (n % concatenation == 0 ); } // Driver code public static void main(String[] args) { // Given number int n = 108 ; // Function call if (isGapful(n)) System.out.print( "Yes" ); else System.out.print( "No" ); } } // This code is contributed by Pratima Pandey |
Python3
# Python3 program for the above approach import math # Find the first digit def firstDigit(n): # Find total number of digits - 1 digits = math.log10(n) # Find first digit n = (n / math. pow ( 10 , digits)) # Return first digit return n # Find the last digit def lastDigit(n): # return the last digit return (n % 10 ) # A function to check Gapful numbers def isGapful(n): concatenation = (firstDigit(n) * 10 ) + \ lastDigit(n) # Return true if n is gapful number return (n % concatenation) # Driver Code if __name__ = = '__main__' : # Given Number n = 108 # Function Call if (isGapful(n)): print ( "Yes" ) else : print ( "No" ) # This code is contributed by Ritik Bansal |
C#
// C# program for the above approach using System; class GFG{ // Find the first digit static int firstDigit( int n) { // Find total number of digits - 1 int digits = ( int )(Math.Log(n) / Math.Log(10)); // Find first digit n = ( int )(n / Math.Pow(10, digits)); // Return first digit return n; } // Find the last digit static int lastDigit( int n) { // Return the last digit return (n % 10); } // A function to check Gapful numbers static bool isGapful( int n) { int first_dig = firstDigit(n); int last_dig = lastDigit(n); int concatenation = first_dig * 10 + last_dig; // Return true if n is gapful number return (n % concatenation == 0); } // Driver code public static void Main() { // Given number int n = 108; // Function call if (isGapful(n)) Console.Write( "Yes" ); else Console.Write( "No" ); } } // This code is contributed by Code_Mech |
Javascript
<script> // Javascript program for the above approach // Find the first digit function firstDigit( n) { // Find total number of digits - 1 let digits = parseInt( (Math.log(n) / Math.log(10))); // Find first digit n = parseInt( (n / Math.pow(10, digits))); // Return first digit return n; } // Find the last digit function lastDigit( n) { // Return the last digit return (n % 10); } // A function to check Gapful numbers function isGapful( n) { let first_dig = firstDigit(n); let last_dig = lastDigit(n); let concatenation = first_dig * 10 + last_dig; // Return true if n is gapful number return (n % concatenation == 0); } // Driver code // Given number let n = 108; // Function call if (isGapful(n)) document.write( "Yes" ); else document.write( "No" ); // This code is contributed by aashish1995 </script> |
Yes
Time Complexity: O(1)
Reference: https://oeis.org/A108343
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!