Gapful Numbers

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Yes

Time Complexity: O(1)
Reference: https://oeis.org/A108343

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.