Self dividing numbers
Self dividing numbers are positive integers that have the unique property of being divisible by each of their digits without any remainder. In other words, a self-dividing number is a number that can be divided by all its individual digits. These numbers are interesting mathematical constructs and can be useful in various problem-solving scenarios.
Problem Statement
Given a positive integer, we need to determine whether it is a self-dividing number or not. The task is to write a C program that takes an integer as input and checks if it is self-dividing. If the number is self-dividing, the program should print "Is Self Divisible Number"; otherwise, it should print "Is Not Self Divisible Number".
Example
Let's take a few examples to illustrate self-dividing numbers:
-
Number: 12
- Explanation: The number 12 has two digits, 1 and 2. It is divisible by both 1 and 2 without any remainder. Hence, it is a self-dividing number.
- Output: Given Number: 12, Is Self Divisible Number
-
Number: 19
- Explanation: The number 19 has two digits, 1 and 9. It is divisible by 1 without any remainder, but it is not divisible by 9. Hence, it is not a self-dividing number.
- Output: Given Number: 19, Is Not Self Divisible Number
-
Number: 728
- Explanation: The number 728 has three digits, 7, 2, and 8. It is divisible by all three digits without any remainder. Hence, it is a self-dividing number.
- Output: Given Number: 728, Is Self Divisible Number
-
Number: 23
- Explanation: The number 23 has two digits, 2 and 3. It is divisible by 2 without any remainder, but it is not divisible by 3. Hence, it is not a self-dividing number.
- Output: Given Number: 23, Is Not Self Divisible Number
Pseudocode
isSelfDivisible(num):
temp = num
result = 1
if num > 0:
while temp > 0 and result == 1:
last_digit = temp % 10
if last_digit == 0 or num % last_digit != 0:
result = 0
else:
temp = temp / 10
else:
result = 0
print("Given Number:", num)
if result == 1:
print("Is Self Divisible Number")
else:
print("Is Not Self Divisible Number")
main():
isSelfDivisible(12)
isSelfDivisible(19)
isSelfDivisible(728)
isSelfDivisible(23)
Algorithm Explanation
- Start with the given positive integer
num
. - Initialize a variable
temp
to store a temporary copy of the number. Also, initializeresult
to 1. - Check if the input number
num
is greater than 0. If it is not, then the number cannot be self-dividing, so setresult
to 0. - If
num
is greater than 0, enter the loop to check each digit of the number. - In each iteration, extract the last digit of
temp
using the expressiontemp % 10
. - Check if the last digit is zero or if the original number
num
is not divisible by this last digit (usingnum % (temp % 10)
). If either of these conditions is true, then setresult
to 0, as the number is not self-dividing. - If the last digit is not zero and the number is divisible by it, update the
temp
by removing the last digit (temp = temp / 10). - Repeat steps 5 to 7 until all digits of the number have been checked.
- After the loop ends,
result
will be 1 if the number is self-dividing, and 0 otherwise. - Print the result accordingly.
Code Solution
Here given code implementation process.
/*
C program for
Self dividing numbers
*/
#include <stdio.h>
void isSelfDivisible(int num)
{
int temp = num;
int result = 1;
if (num > 0)
{
while (temp > 0 && result == 1)
{
if (((temp % 10) == 0) || ((num % (temp % 10)) != 0))
{
// When numbers are contains zero or
// number digit is not divisible itself
result = 0;
}
else
{
// Remove last digit
temp = temp / 10;
}
}
}
else
{
result = 0;
}
printf("\n Given Number : %d", num);
if (result == 1)
{
printf("\n Is Self Divisible Number");
}
else
{
printf("\n Is Not Self Divisible Number");
}
}
int main(int argc, char
const *argv[])
{
// Test
isSelfDivisible(12);
isSelfDivisible(19);
isSelfDivisible(728);
isSelfDivisible(23);
return 0;
}
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
/*
Java program for
Self dividing numbers
*/
public class Divisible
{
public void isSelfDivisible(int num)
{
int temp = num;
boolean result = true;
if (num > 0)
{
while (temp > 0 && result == true)
{
if (((temp % 10) == 0) || ((num % (temp % 10)) != 0))
{
// When numbers are contains zero or
// number digit is not divisible itself
result = false;
}
else
{
// Remove last digit
temp = temp / 10;
}
}
}
else
{
result = false;
}
System.out.print("\n Given Number : " + num );
if (result == true)
{
System.out.print("\n Is Self Divisible Number");
}
else
{
System.out.print("\n Is Not Self Divisible Number");
}
}
public static void main(String[] args)
{
Divisible task = new Divisible();
// Test
task.isSelfDivisible(12);
task.isSelfDivisible(19);
task.isSelfDivisible(728);
task.isSelfDivisible(23);
}
}
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Self dividing numbers
*/
class Divisible
{
public: void isSelfDivisible(int num)
{
int temp = num;
bool result = true;
if (num > 0)
{
while (temp > 0 && result == true)
{
if (((temp % 10) == 0) || ((num % (temp % 10)) != 0))
{
// When numbers are contains zero or
// number digit is not divisible itself
result = false;
}
else
{
// Remove last digit
temp = temp / 10;
}
}
}
else
{
result = false;
}
cout << "\n Given Number : " << num;
if (result == true)
{
cout << "\n Is Self Divisible Number";
}
else
{
cout << "\n Is Not Self Divisible Number";
}
}
};
int main()
{
Divisible *task = new Divisible();
// Test
task->isSelfDivisible(12);
task->isSelfDivisible(19);
task->isSelfDivisible(728);
task->isSelfDivisible(23);
return 0;
}
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
// Include namespace system
using System;
/*
Csharp program for
Self dividing numbers
*/
public class Divisible
{
public void isSelfDivisible(int num)
{
int temp = num;
Boolean result = true;
if (num > 0)
{
while (temp > 0 && result == true)
{
if (((temp % 10) == 0) || ((num % (temp % 10)) != 0))
{
// When numbers are contains zero or
// number digit is not divisible itself
result = false;
}
else
{
// Remove last digit
temp = temp / 10;
}
}
}
else
{
result = false;
}
Console.Write("\n Given Number : " + num);
if (result == true)
{
Console.Write("\n Is Self Divisible Number");
}
else
{
Console.Write("\n Is Not Self Divisible Number");
}
}
public static void Main(String[] args)
{
Divisible task = new Divisible();
// Test
task.isSelfDivisible(12);
task.isSelfDivisible(19);
task.isSelfDivisible(728);
task.isSelfDivisible(23);
}
}
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
package main
import "fmt"
/*
Go program for
Self dividing numbers
*/
func isSelfDivisible(num int) {
var temp int = num
var result bool = true
if num > 0 {
for (temp > 0 && result == true) {
if ((temp % 10) == 0) || ((num % (temp % 10)) != 0) {
// When numbers are contains zero or
// number digit is not divisible itself
result = false
} else {
// Remove last digit
temp = temp / 10
}
}
} else {
result = false
}
fmt.Print("\n Given Number : ", num)
if result == true {
fmt.Print("\n Is Self Divisible Number")
} else {
fmt.Print("\n Is Not Self Divisible Number")
}
}
func main() {
// Test
isSelfDivisible(12)
isSelfDivisible(19)
isSelfDivisible(728)
isSelfDivisible(23)
}
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
<?php
/*
Php program for
Self dividing numbers
*/
class Divisible
{
public function isSelfDivisible($num)
{
$temp = $num;
$result = true;
if ($num > 0)
{
while ($temp > 0 && $result == true)
{
if ((($temp % 10) == 0) || (($num % ($temp % 10)) != 0))
{
// When numbers are contains zero or
// number digit is not divisible itself
$result = false;
}
else
{
// Remove last digit
$temp = (int)($temp / 10);
}
}
}
else
{
$result = false;
}
echo("\n Given Number : ".$num);
if ($result == true)
{
echo("\n Is Self Divisible Number");
}
else
{
echo("\n Is Not Self Divisible Number");
}
}
}
function main()
{
$task = new Divisible();
// Test
$task->isSelfDivisible(12);
$task->isSelfDivisible(19);
$task->isSelfDivisible(728);
$task->isSelfDivisible(23);
}
main();
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
/*
Node JS program for
Self dividing numbers
*/
class Divisible
{
isSelfDivisible(num)
{
var temp = num;
var result = true;
if (num > 0)
{
while (temp > 0 && result == true)
{
if (((temp % 10) == 0) || ((num % (temp % 10)) != 0))
{
// When numbers are contains zero or
// number digit is not divisible itself
result = false;
}
else
{
// Remove last digit
temp = parseInt(temp / 10);
}
}
}
else
{
result = false;
}
process.stdout.write("\n Given Number : " + num);
if (result == true)
{
process.stdout.write("\n Is Self Divisible Number");
}
else
{
process.stdout.write("\n Is Not Self Divisible Number");
}
}
}
function main()
{
var task = new Divisible();
// Test
task.isSelfDivisible(12);
task.isSelfDivisible(19);
task.isSelfDivisible(728);
task.isSelfDivisible(23);
}
main();
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
# Python 3 program for
# Self dividing numbers
class Divisible :
def isSelfDivisible(self, num) :
temp = num
result = True
if (num > 0) :
while (temp > 0 and result == True) :
if (((temp % 10) == 0) or((num % (temp % 10)) != 0)) :
# When numbers are contains zero or
# number digit is not divisible itself
result = False
else :
# Remove last digit
temp = int(temp / 10)
else :
result = False
print("\n Given Number : ", num, end = "")
if (result == True) :
print("\n Is Self Divisible Number", end = "")
else :
print("\n Is Not Self Divisible Number", end = "")
def main() :
task = Divisible()
# Test
task.isSelfDivisible(12)
task.isSelfDivisible(19)
task.isSelfDivisible(728)
task.isSelfDivisible(23)
if __name__ == "__main__": main()
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
# Ruby program for
# Self dividing numbers
class Divisible
def isSelfDivisible(num)
temp = num
result = true
if (num > 0)
while (temp > 0 && result == true)
if (((temp % 10) == 0) || ((num % (temp % 10)) != 0))
# When numbers are contains zero or
# number digit is not divisible itself
result = false
else
# Remove last digit
temp = temp / 10
end
end
else
result = false
end
print("\n Given Number : ", num)
if (result == true)
print("\n Is Self Divisible Number")
else
print("\n Is Not Self Divisible Number")
end
end
end
def main()
task = Divisible.new()
# Test
task.isSelfDivisible(12)
task.isSelfDivisible(19)
task.isSelfDivisible(728)
task.isSelfDivisible(23)
end
main()
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
/*
Scala program for
Self dividing numbers
*/
class Divisible()
{
def isSelfDivisible(num: Int): Unit = {
var temp: Int = num;
var result: Boolean = true;
if (num > 0)
{
while (temp > 0 && result == true)
{
if (((temp % 10) == 0) || ((num % (temp % 10)) != 0))
{
// When numbers are contains zero or
// number digit is not divisible itself
result = false;
}
else
{
// Remove last digit
temp = temp / 10;
}
}
}
else
{
result = false;
}
print("\n Given Number : " + num);
if (result == true)
{
print("\n Is Self Divisible Number");
}
else
{
print("\n Is Not Self Divisible Number");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Divisible = new Divisible();
// Test
task.isSelfDivisible(12);
task.isSelfDivisible(19);
task.isSelfDivisible(728);
task.isSelfDivisible(23);
}
}
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
/*
Swift 4 program for
Self dividing numbers
*/
class Divisible
{
func isSelfDivisible(_ num: Int)
{
var temp: Int = num;
var result: Bool = true;
if (num > 0)
{
while (temp > 0 && result == true)
{
if (((temp % 10) == 0) || ((num % (temp % 10)) != 0))
{
// When numbers are contains zero or
// number digit is not divisible itself
result = false;
}
else
{
// Remove last digit
temp = temp / 10;
}
}
}
else
{
result = false;
}
print("\n Given Number : ", num, terminator: "");
if (result == true)
{
print("\n Is Self Divisible Number", terminator: "");
}
else
{
print("\n Is Not Self Divisible Number", terminator: "");
}
}
}
func main()
{
let task: Divisible = Divisible();
// Test
task.isSelfDivisible(12);
task.isSelfDivisible(19);
task.isSelfDivisible(728);
task.isSelfDivisible(23);
}
main();
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
/*
Kotlin program for
Self dividing numbers
*/
class Divisible
{
fun isSelfDivisible(num: Int): Unit
{
var temp: Int = num;
var result: Boolean = true;
if (num > 0)
{
while (temp > 0 && result == true)
{
if (((temp % 10) == 0) || ((num % (temp % 10)) != 0))
{
// When numbers are contains zero or
// number digit is not divisible itself
result = false;
}
else
{
// Remove last digit
temp = temp / 10;
}
}
}
else
{
result = false;
}
print("\n Given Number : " + num);
if (result == true)
{
print("\n Is Self Divisible Number");
}
else
{
print("\n Is Not Self Divisible Number");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Divisible = Divisible();
// Test
task.isSelfDivisible(12);
task.isSelfDivisible(19);
task.isSelfDivisible(728);
task.isSelfDivisible(23);
}
Output
Given Number : 12
Is Self Divisible Number
Given Number : 19
Is Not Self Divisible Number
Given Number : 728
Is Self Divisible Number
Given Number : 23
Is Not Self Divisible Number
Resultant Output Explanation
-
isSelfDivisible(12);
- The number 12 is a self-dividing number because both its digits (1 and 2) divide the number evenly without any remainder. Hence, the output is:Given Number: 12 Is Self Divisible Number
-
isSelfDivisible(19);
- The number 19 is not a self-dividing number because while it is divisible by 1 without a remainder, it is not divisible by 9. Hence, the output is:Given Number: 19 Is Not Self Divisible Number
-
isSelfDivisible(728);
- The number 728 is a self-dividing number because all three of its digits (7, 2, and 8) divide the number evenly without any remainder. Hence, the output is:Given Number: 728 Is Self Divisible Number
-
isSelfDivisible(23);
- The number 23 is not a self-dividing number because while it is divisible by 2 without a remainder, it is not divisible by 3. Hence, the output is:Given Number: 23 Is Not Self Divisible Number
Time Complexity
-
In the
isSelfDivisible()
function, we have a loop that iterates until the variabletemp
becomes zero or untilresult
is set to 0. The loop is traversing through the digits of the given number. -
In each iteration of the loop, we are performing constant-time operations such as modulo and division (
temp % 10
andtemp / 10
), as well as some comparison operations. -
The number of iterations in the loop is determined by the number of digits in the given number
num
. Let's assume the number of digits innum
isd
. -
The time complexity of modulo and division operations on a number with
d
digits is generally considered to be O(d). -
Therefore, the overall time complexity of the
isSelfDivisible()
function is O(d), whered
is the number of digits in the given input numbernum
. -
In the
main()
function, we are callingisSelfDivisible()
four times with different input numbers. Let's assume the maximum number of digits among these four numbers isd_max
. -
Since we are independently calling
isSelfDivisible()
for each number, the time complexity of the entiremain()
function is the sum of the time complexities of each individual call, which is O(d_max). -
The overall time complexity of the entire C program is also O(d_max).
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