Program to find the number from given holes

Given a number H which represent the total number of holes. The task is to find the smallest number which has that many numbers of holes.

NOTE:

  1. 0, 4, 6, 9 has 1 holes each and 8 has 2 holes in it.
  2. The number should not contain leading zeros.

Examples:

Input: H = 1
Output: 0

Input: H = 5
Output: 488
Explanation:
Number which has 5 holes in it is 488. i.e (1 + 2 + 2)



Refer: Count the number of holes in an integer

Approach:

  1. First of all, Check whether the number of holes given is 0 or 1, if 0 then print 1 and if 1 then print 0.
  2. If number of holes given is more than 1 then divide the number of holes by 2 and store the remainder in ‘rem’ variable. and quotient in ‘quo’ variable.
  3. Now, if value of rem variable is equal to 1 then first print 4 once and then print 8 quo number of times.
  4. Else print 8 only quo number of times.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that will find out
// the number
void printNumber(int holes)
{
  
    // If number of holes
    // equal 0 then return 1
    if (holes == 0)
        cout << "1";
  
    // If number of holes
    // equal 0 then return 0
    else if (holes == 1)
        cout << "0";
  
    // If number of holes
    // is more than 0 or 1.
    else {
        int rem = 0, quo = 0;
  
        rem = holes % 2;
        quo = holes / 2;
  
        // If number of holes is
        // odd
        if (rem == 1)
            cout << "4";
  
        for (int i = 0; i < quo; i++)
            cout << "8";
    }
}
  
// Driver code
int main()
{
    int holes = 3;
  
    // Calling Function
    printNumber(holes);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
import java.io.*;
  
class GFG 
{
          
// Function that will find out
// the number
static void printNumber(int holes)
{
  
    // If number of holes
    // equal 0 then return 1
    if (holes == 0)
        System.out.print("1");
  
    // If number of holes
    // equal 0 then return 0
    else if (holes == 1)
        System.out.print("0");
  
    // If number of holes
    // is more than 0 or 1.
    else
    {
        int rem = 0, quo = 0;
  
        rem = holes % 2;
        quo = holes / 2;
  
        // If number of holes is
        // odd
        if (rem == 1)
            System.out.print("4");
  
        for (int i = 0; i < quo; i++)
                System.out.print("8");
    }
}
  
// Driver code
public static void main (String[] args) 
{
    int holes = 3;
      
    // Calling Function
    printNumber(holes);
}
}
  
// This code is contributed by Sachin.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of 
# the above approach
  
# Function that will find out
# the number
def printNumber(holes):
  
    # If number of holes
    # equal 0 then return 1
    if (holes == 0):
        print("1")
  
    # If number of holes
    # equal 0 then return 0
    elif (holes == 1):
        print("0", end = "")
  
    # If number of holes
    # is more than 0 or 1.
    else:
        rem = 0
        quo = 0
  
        rem = holes % 2
        quo = holes // 2
  
        # If number of holes is
        # odd
        if (rem == 1):
            print("4", end = "")
  
        for i in range(quo):
            print("8", end = "")
  
# Driver code
holes = 3
  
# Calling Function
printNumber(holes)
  
# This code is contributed by Mohit kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
  
class GFG
{
      
// Function that will find out
// the number
static void printNumber(int holes)
{
  
    // If number of holes
    // equal 0 then return 1
    if (holes == 0)
        Console.Write ("1");
  
    // If number of holes
    // equal 0 then return 0
    else if (holes == 1)
        Console.Write ("0");
  
    // If number of holes
    // is more than 0 or 1.
    else
    {
        int rem = 0, quo = 0;
  
        rem = holes % 2;
        quo = holes / 2;
  
        // If number of holes is
        // odd
        if (rem == 1)
        Console.Write ("4");
  
        for (int i = 0; i < quo; i++)
            Console.Write ("8");
    }
}
  
// Driver code
static public void Main ()
{
    int holes = 3;
      
    // Calling Function
    printNumber(holes);
}
}
  
// This code is contributed by jit_t

chevron_right


Output:

48

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.