# Check if all prefixes of a number is divisible by remaining count of digits

• Last Updated : 24 Jun, 2021

Given a number N, the task is to check if for every value of i (0 <= i <= len), the first i digits of a number is divisible by (len – i + 1) or not, where len is the number of digits in N. If found to be true, then print “Yes”. Otherwise, print “No”.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input: N = 52248.
Output: Yes
Explanation:

• 5 is divisible by 5
• 52 is divisible by 4
• 522 is divisible by 3
• 5224 is divisible by 2
• 52248 is divisible by 1

Input: N = 59268
Output : No

Approach: The idea is to traverse all the prefixes of the given number and for each prefix, check if it is satisfies the condition or not.

Follow the steps below to solve the problem:

• Initialize a variable, say i as 1, to maintain the value of (len – i + 1).
• Iterate while n is greater than 0.
• If N is not divisible by i, then return false.
• Otherwise, divide N by 10 and increase the value of i by 1.
• Finally, return true.

Below is the implementation of the above approach.

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to check if all prefixes``// of a number is divisible by``// remaining count of digits or not``bool` `prefixDivisble(``int` `n)``{``    ``int` `i = 1;` `    ``while` `(n > 0) {` `        ``// Traverse and check divisibility``        ``// for each updated number``        ``if` `(n % i != 0)``            ``return` `false``;` `        ``// Update the original number``        ``n = n / 10;` `        ``i++;``    ``}` `    ``return` `true``;``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``int` `n = 52248;` `    ``// Function Call``    ``if` `(prefixDivisble(n))``        ``cout << ``"Yes"` `<< endl;``    ``else``        ``cout << ``"No"` `<< endl;` `    ``return` `0;``}`

## Java

 `// Java program  for the above approach` `import` `java.io.*;` `class` `GFG{``    ` `// Function to check if all prefixes``// of a number is divisible by``// remaining count of digits or not``public` `static` `boolean` `prefixDivisble(``int` `n)``{``    ``int` `i = ``1``;` `    ``while` `(n > ``0``)``    ``{``        ` `        ``// Traverse and check divisibility``        ``// for each updated number``        ``if` `(n % i != ``0``)``            ``return` `false``;` `        ``// Update the original number``        ``n = n / ``10``;` `        ``i++;``    ``}``    ``return` `true``;``}` `// Driver Code``public` `static` `void` `main (String[] args)``{` `    ``// Given Input``    ``int` `n = ``52248``;``    ` `    ``// Function Call``    ``if` `(prefixDivisble(n))``        ``System.out.println(``"Yes"``);``    ``else``        ``System.out.println(``"No"``);``}``}` `// This code is contributed by lokeshpotta20.`

## Python3

 `# Python3 program for the above approach` `# Function to check if all prefixes of``# a number is divisible by remaining count``# of digits or not``def` `prefixDivisble(n):``    ` `    ``i ``=` `1``    ` `    ``while` `n > ``0``:``        ` `        ``# Traverse and check divisibility``        ``# for each updated number``        ``if` `n ``%` `i !``=` `0``:``            ``return` `False``        ` `        ``# Update the original number``        ``n ``=` `n ``/``/` `10``        ``i ``+``=` `1``    ` `    ``return` `True` `# Driver Code` `# Given Input``n ``=` `52248` `# Function Call``if` `(prefixDivisble(n)):``   ``print``(``"Yes"``)``else``:``   ``print``(``"No"``)` `# This code is contributed by abhivick07`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{`` ` `// Function to check if all prefixes``// of a number is divisible by``// remaining count of digits or not``static` `bool` `prefixDivisble(``int` `n)``{``    ``int` `i = 1;` `    ``while` `(n > 0)``    ``{``        ` `        ``// Traverse and check divisibility``        ``// for each updated number``        ``if` `(n % i != 0)``            ``return` `false``;` `        ``// Update the original number``        ``n = n / 10;` `        ``i++;``    ``}``    ``return` `true``;``}` `// Driver Code``public` `static` `void` `Main()``{``    ` `    ``// Given Input``    ``int` `n = 52248;` `    ``// Function Call``    ``if` `(prefixDivisble(n))``        ``Console.Write(``"Yes"``);``    ``else``        ``Console.Write(``"No"``);``}``}` `// This code is contributed by ipg2016107`

## Javascript

 ``
Output
`Yes`

Time Complexity : O(len) where len is the number of digits in N.
Auxiliary Space : O(1)

My Personal Notes arrow_drop_up