Check whether a given number is an Polydivisible number or not
Here given code implementation process.
// C program for
// Check whether a given number is an Polydivisible number or not
#include <stdio.h>
void isPolydivisible(int number)
{
/*
Properties of polydivisible number
──────────────────────────────────
➀ Its first digit a is not 0.
➁ The number formed by its first two digits ab is a multiple of 2.
➂ The number formed by its first three digits abc is a multiple of 3.
➃ The number formed by its first four digits abcd is a multiple of 4.
➄ etc.
───────────────────────────────────
*/
int status = 0;
if (number > 0)
{
int n = number;
int length = 0;
// Count the number of digits in given number
while (n > 0)
{
n /= 10;
length++;
}
if (n == 1)
{
// When its single digit
status = 1;
}
else
{
// Auxiliary space to collecting digits
int data[length];
int i = length - 1;
int num = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n /= 10;
i--;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = 1;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status == 1)
{
// Combine digit
num = (num *10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = 0;
}
i++;
}
}
}
if (status == 1)
{
printf("\n Number %d is Polydivisible", number);
}
else
{
printf("\n Number %d is not Polydivisible", number);
}
}
int main(int argc, char const *argv[])
{
// Test Case of Base 10
isPolydivisible(98);
isPolydivisible(161);
isPolydivisible(33);
isPolydivisible(78);
isPolydivisible(180);
return 0;
}
input
Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
/*
Java Program for
Check whether a given number is an Polydivisible number or not
*/
public class Polydivisible
{
public void isPolydivisible(int number)
{
// Result indicator
boolean status = false;
// Properties of polydivisible number
// ──────────────────────────────────
// ➀ Its first digit a is not 0.
// ➁ The number formed by its first two digits ab is a multiple of 2.
// ➂ The number formed by its first three digits abc is a multiple of 3.
// ➃ The number formed by its first four digits abcd is a multiple of 4.
// ➄ etc.
// ───────────────────────────────────
if (number > 0)
{
int n = number;
int length = 0;
// Count the number of digits in given number
while (n > 0)
{
n /= 10;
length++;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
int[] data = new int[length];
int i = length - 1;
int num = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n /= 10;
i--;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num * 10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i++;
}
}
}
if (status)
{
System.out.println(" Number " + number + " is Polydivisible");
}
else
{
System.out.println(" Number " + number + " is not Polydivisible");
}
}
public static void main(String[] args)
{
Polydivisible task = new Polydivisible();
// Test Case of Base 10
task.isPolydivisible(98);
task.isPolydivisible(161);
task.isPolydivisible(33);
task.isPolydivisible(78);
task.isPolydivisible(180);
}
}
input
Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible
{
public: void isPolydivisible(int number)
{
// Result indicator
bool status = false;
// Properties of polydivisible number
// ──────────────────────────────────
// ➀ Its first digit a is not 0.
// ➁ The number formed by its first two digits ab is a multiple of 2.
// ➂ The number formed by its first three digits abc is a multiple of 3.
// ➃ The number formed by its first four digits abcd is a multiple of 4.
// ➄ etc.
// ───────────────────────────────────
if (number > 0)
{
int n = number;
int length = 0;
// Count the number of digits in given number
while (n > 0)
{
n /= 10;
length++;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
int *data = new int[length];
int i = length - 1;
int num = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n /= 10;
i--;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num *10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i++;
}
free(data);
}
}
if (status)
{
cout << " Number " << number << " is Polydivisible" << endl;
}
else
{
cout << " Number " << number << " is not Polydivisible" << endl;
}
}
};
int main()
{
Polydivisible *task = new Polydivisible();
// Test Case of Base 10
task->isPolydivisible(98);
task->isPolydivisible(161);
task->isPolydivisible(33);
task->isPolydivisible(78);
task->isPolydivisible(180);
return 0;
}
input
Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
// Include namespace system
using System;
/*
Csharp Program for
Check whether a given number is an Polydivisible number or not
*/
public class Polydivisible
{
public void isPolydivisible(int number)
{
// Result indicator
Boolean status = false;
// Properties of polydivisible number
// ──────────────────────────────────
// ➀ Its first digit a is not 0.
// ➁ The number formed by its first two digits ab is a multiple of 2.
// ➂ The number formed by its first three digits abc is a multiple of 3.
// ➃ The number formed by its first four digits abcd is a multiple of 4.
// ➄ etc.
// ───────────────────────────────────
if (number > 0)
{
int n = number;
int length = 0;
// Count the number of digits in given number
while (n > 0)
{
n /= 10;
length++;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
int[] data = new int[length];
int i = length - 1;
int num = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n /= 10;
i--;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num * 10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i++;
}
}
}
if (status)
{
Console.WriteLine(" Number " + number + " is Polydivisible");
}
else
{
Console.WriteLine(" Number " + number + " is not Polydivisible");
}
}
public static void Main(String[] args)
{
Polydivisible task = new Polydivisible();
// Test Case of Base 10
task.isPolydivisible(98);
task.isPolydivisible(161);
task.isPolydivisible(33);
task.isPolydivisible(78);
task.isPolydivisible(180);
}
}
input
Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
<?php
/*
Php Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible
{
public function isPolydivisible($number)
{
// Result indicator
$status = false;
// Properties of polydivisible number
// ──────────────────────────────────
// ➀ Its first digit a is not 0.
// ➁ The number formed by its first two digits ab is a multiple of 2.
// ➂ The number formed by its first three digits abc is a multiple of 3.
// ➃ The number formed by its first four digits abcd is a multiple of 4.
// ➄ etc.
// ───────────────────────────────────
if ($number > 0)
{
$n = $number;
$length = 0;
// Count the number of digits in given number
while ($n > 0)
{
$n = (int)($n / 10);
$length++;
}
if ($n == 1)
{
// When its single digit
$status = true;
}
else
{
// Auxiliary space to collecting digits
$data = array_fill(0, $length, 0);
$i = $length - 1;
$num = 0;
$n = $number;
// Collect number digit
while ($n > 0)
{
$data[$i] = $n % 10;
$n = (int)($n / 10);
$i--;
}
// Get first digit
$num = $data[0];
$i = 1;
if ($num != 0)
{
// First case
$status = true;
}
// Check if digit is divisible by its size for 2...length-1
while ($i < $length && $status)
{
// Combine digit
$num = ($num * 10) + $data[$i];
if (($num % ($i + 1)) != 0)
{
// When n digit is not divisible by its size
$status = false;
}
$i++;
}
}
}
if ($status)
{
echo " Number ".$number.
" is Polydivisible".
"\n";
}
else
{
echo " Number ".$number.
" is not Polydivisible".
"\n";
}
}
}
function main()
{
$task = new Polydivisible();
// Test Case of Base 10
$task->isPolydivisible(98);
$task->isPolydivisible(161);
$task->isPolydivisible(33);
$task->isPolydivisible(78);
$task->isPolydivisible(180);
}
main();
input
Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
/*
Node JS Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible
{
isPolydivisible(number)
{
// Result indicator
var status = false;
// Properties of polydivisible number
// ──────────────────────────────────
// ➀ Its first digit a is not 0.
// ➁ The number formed by its first two digits ab is a multiple of 2.
// ➂ The number formed by its first three digits abc is a multiple of 3.
// ➃ The number formed by its first four digits abcd is a multiple of 4.
// ➄ etc.
// ───────────────────────────────────
if (number > 0)
{
var n = number;
var length = 0;
// Count the number of digits in given number
while (n > 0)
{
n = parseInt(n / 10);
length++;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
var data = Array(length).fill(0);
var i = length - 1;
var num = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n = parseInt(n / 10);
i--;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num * 10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i++;
}
}
}
if (status)
{
console.log(" Number " + number + " is Polydivisible");
}
else
{
console.log(" Number " + number + " is not Polydivisible");
}
}
}
function main()
{
var task = new Polydivisible();
// Test Case of Base 10
task.isPolydivisible(98);
task.isPolydivisible(161);
task.isPolydivisible(33);
task.isPolydivisible(78);
task.isPolydivisible(180);
}
main();
input
Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
# Python 3 Program for
# Check whether a given number is an Polydivisible number or not
class Polydivisible :
def isPolydivisible(self, number) :
status = False
# Properties of polydivisible number
# ──────────────────────────────────
# ➀ Its first digit a is not 0.
# ➁ The number formed by its first two digits ab is a multiple of 2.
# ➂ The number formed by its first three digits abc is a multiple of 3.
# ➃ The number formed by its first four digits abcd is a multiple of 4.
# ➄ etc.
# ───────────────────────────────────
if (number > 0) :
n = number
length = 0
# Count the number of digits in given number
while (n > 0) :
n = int(n / 10)
length += 1
if (n == 1) :
# When its single digit
status = True
else :
data = [0] * (length)
i = length - 1
num = 0
n = number
# Collect number digit
while (n > 0) :
data[i] = n % 10
n = int(n / 10)
i -= 1
# Get first digit
num = data[0]
i = 1
if (num != 0) :
# First case
status = True
# Check if digit is divisible by its size for 2...length-1
while (i < length and status) :
# Combine digit
num = (num * 10) + data[i]
if ((num % (i + 1)) != 0) :
# When n digit is not divisible by its size
status = False
i += 1
if (status) :
print(" Number ", number ," is Polydivisible")
else :
print(" Number ", number ," is not Polydivisible")
def main() :
task = Polydivisible()
# Test Case of Base 10
task.isPolydivisible(98)
task.isPolydivisible(161)
task.isPolydivisible(33)
task.isPolydivisible(78)
task.isPolydivisible(180)
if __name__ == "__main__": main()
input
Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
# Ruby Program for
# Check whether a given number is an Polydivisible number or not
class Polydivisible
def isPolydivisible(number)
# Result indicator
status = false
# Properties of polydivisible number
# ──────────────────────────────────
# ➀ Its first digit a is not 0.
# ➁ The number formed by its first two digits ab is a multiple of 2.
# ➂ The number formed by its first three digits abc is a multiple of 3.
# ➃ The number formed by its first four digits abcd is a multiple of 4.
# ➄ etc.
# ───────────────────────────────────
if (number > 0)
n = number
length = 0
# Count the number of digits in given number
while (n > 0)
n = n / 10
length += 1
end
if (n == 1)
# When its single digit
status = true
else
# Auxiliary space to collecting digits
data = Array.new(length) {0}
i = length - 1
num = 0
n = number
# Collect number digit
while (n > 0)
data[i] = n % 10
n = n / 10
i -= 1
end
# Get first digit
num = data[0]
i = 1
if (num != 0)
# First case
status = true
end
# Check if digit is divisible by its size for 2...length-1
while (i < length && status)
# Combine digit
num = (num * 10) + data[i]
if ((num % (i + 1)) != 0)
# When n digit is not divisible by its size
status = false
end
i += 1
end
end
end
if (status)
print(" Number ", number ," is Polydivisible", "\n")
else
print(" Number ", number ," is not Polydivisible", "\n")
end
end
end
def main()
task = Polydivisible.new()
# Test Case of Base 10
task.isPolydivisible(98)
task.isPolydivisible(161)
task.isPolydivisible(33)
task.isPolydivisible(78)
task.isPolydivisible(180)
end
main()
input
Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
/*
Scala Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible()
{
def isPolydivisible(number: Int): Unit = {
// Result indicator
var status: Boolean = false;
// Properties of polydivisible number
// ──────────────────────────────────
// ➀ Its first digit a is not 0.
// ➁ The number formed by its first two digits ab is a multiple of 2.
// ➂ The number formed by its first three digits abc is a multiple of 3.
// ➃ The number formed by its first four digits abcd is a multiple of 4.
// ➄ etc.
// ───────────────────────────────────
if (number > 0)
{
var n: Int = number;
var length: Int = 0;
// Count the number of digits in given number
while (n > 0)
{
n = (n / 10).toInt;
length += 1;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
var data: Array[Int] = Array.fill[Int](length)(0);
var i: Int = length - 1;
var num: Int = 0;
n = number;
// Collect number digit
while (n > 0)
{
data(i) = n % 10;
n = (n / 10).toInt;
i -= 1;
}
// Get first digit
num = data(0);
i = 1;
if (num != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num * 10) + data(i);
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i += 1;
}
}
}
if (status)
{
println(" Number " + number + " is Polydivisible");
}
else
{
println(" Number " + number + " is not Polydivisible");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Polydivisible = new Polydivisible();
// Test Case of Base 10
task.isPolydivisible(98);
task.isPolydivisible(161);
task.isPolydivisible(33);
task.isPolydivisible(78);
task.isPolydivisible(180);
}
}
input
Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
/*
Swift 4 Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible
{
func isPolydivisible(_ number: Int)
{
// Result indicator
var status: Bool = false;
// Properties of polydivisible number
// ──────────────────────────────────
// ➀ Its first digit a is not 0.
// ➁ The number formed by its first two digits ab is a multiple of 2.
// ➂ The number formed by its first three digits abc is a multiple of 3.
// ➃ The number formed by its first four digits abcd is a multiple of 4.
// ➄ etc.
// ───────────────────────────────────
if (number > 0)
{
var n: Int = number;
var length: Int = 0;
// Count the number of digits in given number
while (n > 0)
{
n = n / 10;
length += 1;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
var data: [Int] = Array(repeating: 0, count: length);
var i: Int = length - 1;
var num: Int = 0;
n = number;
// Collect number digit
while (n > 0)
{
data[i] = n % 10;
n = n / 10;
i -= 1;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = true;
}
// Check if digit is divisible by its size for 2...length-1
while (i < length && status)
{
// Combine digit
num = (num * 10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i += 1;
}
}
}
if (status)
{
print(" Number ", number ," is Polydivisible");
}
else
{
print(" Number ", number ," is not Polydivisible");
}
}
}
func main()
{
let task: Polydivisible = Polydivisible();
// Test Case of Base 10
task.isPolydivisible(98);
task.isPolydivisible(161);
task.isPolydivisible(33);
task.isPolydivisible(78);
task.isPolydivisible(180);
}
main();
input
Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
/*
Kotlin Program for
Check whether a given number is an Polydivisible number or not
*/
class Polydivisible
{
fun isPolydivisible(number: Int): Unit
{
// Result indicator
var status: Boolean = false;
// Properties of polydivisible number
// ──────────────────────────────────
// ➀ Its first digit a is not 0.
// ➁ The number formed by its first two digits ab is a multiple of 2.
// ➂ The number formed by its first three digits abc is a multiple of 3.
// ➃ The number formed by its first four digits abcd is a multiple of 4.
// ➄ etc.
// ───────────────────────────────────
if (number > 0)
{
var n: Int = number;
var length: Int = 0;
while (n > 0)
{
n = n / 10;
length += 1;
}
if (n == 1)
{
// When its single digit
status = true;
}
else
{
// Auxiliary space to collecting digits
val data: Array < Int > = Array(length)
{
0
};
var i: Int = length - 1;
var num: Int ;
n = number;
while (n > 0)
{
data[i] = n % 10;
n = n / 10;
i -= 1;
}
// Get first digit
num = data[0];
i = 1;
if (num != 0)
{
// First case
status = true;
}
while (i < length && status)
{
// Combine digit
num = (num * 10) + data[i];
if ((num % (i + 1)) != 0)
{
// When n digit is not divisible by its size
status = false;
}
i += 1;
}
}
}
if (status)
{
println(" Number " + number + " is Polydivisible");
}
else
{
println(" Number " + number + " is not Polydivisible");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Polydivisible = Polydivisible();
// Test Case of Base 10
task.isPolydivisible(98);
task.isPolydivisible(161);
task.isPolydivisible(33);
task.isPolydivisible(78);
task.isPolydivisible(180);
}
input
Number 98 is Polydivisible
Number 161 is not Polydivisible
Number 33 is not Polydivisible
Number 78 is Polydivisible
Number 180 is Polydivisible
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment