Sunday, October 6, 2024
Google search engine
HomeData Modelling & AIFind N digits number which is divisible by D

Find N digits number which is divisible by D

Given N   and D  . The task is to find an N digit number that is divisible by D ( 2 <= D <= 10). If it is not possible, then print Impossible. 

Examples

Input : N = 2 and D = 2
Output : 20

Input : N = 1 and D = 10
Output : Impossible

Approach: There are two conditions, D=10 and D not equal to 10. If D = 10 and N = 1 then the only answer is not possible and in all other conditions, the answer will be possible. 

1. If D is 10,
   Print 1 followed by n-1 times zero.
2. If D is not 10
   Print D followed by n-1 times zero

Below is the implementation of the above approach:  

C++




// CPP program to Find N digits
// number which is divisible by D
#include <bits/stdc++.h>
using namespace std;
 
// Function to return N digits
// number which is divisible by D
string findNumber(int n, int d)
{
    // to store answer
    string ans = "";
 
    if (d != 10) {
        ans += to_string(d);
        for (int i = 1; i < n; i++)
            ans += '0';
    }
    else {
        if (n == 1)
            ans += "Impossible";
        else {
            ans += '1';
            for (int i = 1; i < n; i++)
                ans += '0';
        }
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int n = 12, d = 3;
 
    cout << findNumber(n, d);
 
    return 0;
}


Java




// Java program to Find N digits
// number which is divisible by D
 
import java.io.*;
 
class GFG {
 
 
// Function to return N digits
// number which is divisible by D
static String findNumber(int n, int d)
{
    // to store answer
    String ans = "";
 
    if (d != 10) {
        ans += Integer.toString(d);
        for (int i = 1; i < n; i++)
            ans += '0';
    }
    else {
        if (n == 1)
            ans += "Impossible";
        else {
            ans += '1';
            for (int i = 1; i < n; i++)
                ans += '0';
        }
    }
 
    return ans;
}
 
// Driver code
 
    public static void main (String[] args) {
            int n = 12, d = 3;
 
    System.out.println(findNumber(n, d));
    }
}
// This code is contributed by anuj_67..


Python 3




# Python 3 program to Find N digits
# number which is divisible by D
 
# Function to return N digits
# number which is divisible by D
def findNumber(n, d):
 
    # to store answer
    ans = ""
 
    if (d != 10) :
        ans += str(d)
        for i in range(1,n):
            ans += '0'
    else :
        if (n == 1):
            ans += "Impossible"
        else :
            ans += '1'
            for i in range(1,n):
                ans += '0'
 
    return ans
 
# Driver code
if __name__ == "__main__":
    n = 12
    d = 3
 
    print(findNumber(n, d))
     
# This code is contributed by
# ChitraNayal


C#




// C# program to Find N digits
// number which is divisible by D
using System;
 
class GFG {
 
// Function to return N digits
// number which is divisible by D
static string findNumber(int n, int d)
{
     
    // to store answer
    string ans = "";
 
    if (d != 10) {
         
        ans += d.ToString();
         
        for (int i = 1; i < n; i++)
            ans += '0';
    }
     
    else {
         
        if (n == 1)
            ans += "Impossible";
             
        else {
             
            ans += '1';
            for (int i = 1; i < n; i++)
                ans += '0';
        }
    }
 
    return ans;
}
 
// Driver code
public static void Main ()
{
     
    int n = 12, d = 3;
    Console.WriteLine(findNumber(n, d));
}
}
 
// This code is contributed by Subhadeep


PHP




<?php
// PHP program to Find N digits
// number which is divisible by D
 
// Function to return N digits
// number which is divisible by D
function findNumber($n, $d)
{
    // to store answer
    $ans = "";
 
    if ($d != 10)
    {
        $ans .= strval($d);
        for($i = 1; $i < $n; $i++)
            $ans .= '0';
    }
    else
    {
        if (n == 1)
            $ans .= "Impossible";
        else
            $ans .= '1';
            for($i = 1; $i < $n; $i++)
                $ans .= '0';
    }
 
    return $ans;
}
 
// Driver code
$n = 12;
$d = 3;
 
print(findNumber($n, $d));
     
// This code is contributed by mits


Javascript




<script>
// Javascript program to Find N digits
// number which is divisible by D
     
// Function to return N digits
// number which is divisible by D
  function findNumber(n,d)
  {
        // to store answer
    let ans = "";
   
    if (d != 10) {
        ans += (d).toString();
        for (let i = 1; i < n; i++)
            ans += '0';
    }
    else {
        if (n == 1)
            ans += "Impossible";
        else {
            ans += '1';
            for (let i = 1; i < n; i++)
                ans += '0';
        }
      }
   
      return ans;
  }
     
  // Driver code
  let n = 12, d = 3;
  document.write(findNumber(n, d));
 
 
// This code is contributed by avanitrachhadiya2155
</script>


Output: 

300000000000

 

Time Complexity: O(n)
Auxiliary Space: O(n)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments