Skip to main content

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:

  1. 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
  2. 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
  3. 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
  4. 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

  1. Start with the given positive integer num.
  2. Initialize a variable temp to store a temporary copy of the number. Also, initialize result to 1.
  3. Check if the input number num is greater than 0. If it is not, then the number cannot be self-dividing, so set result to 0.
  4. If num is greater than 0, enter the loop to check each digit of the number.
  5. In each iteration, extract the last digit of temp using the expression temp % 10.
  6. Check if the last digit is zero or if the original number num is not divisible by this last digit (using num % (temp % 10)). If either of these conditions is true, then set result to 0, as the number is not self-dividing.
  7. 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).
  8. Repeat steps 5 to 7 until all digits of the number have been checked.
  9. After the loop ends, result will be 1 if the number is self-dividing, and 0 otherwise.
  10. 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

  1. 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
    
  2. 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
  3. 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
    
  4. 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

  1. In the isSelfDivisible() function, we have a loop that iterates until the variable temp becomes zero or until result is set to 0. The loop is traversing through the digits of the given number.

  2. In each iteration of the loop, we are performing constant-time operations such as modulo and division (temp % 10 and temp / 10), as well as some comparison operations.

  3. 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 in num is d.

  4. The time complexity of modulo and division operations on a number with d digits is generally considered to be O(d).

  5. Therefore, the overall time complexity of the isSelfDivisible() function is O(d), where d is the number of digits in the given input number num.

  6. In the main() function, we are calling isSelfDivisible() four times with different input numbers. Let's assume the maximum number of digits among these four numbers is d_max.

  7. Since we are independently calling isSelfDivisible() for each number, the time complexity of the entire main() function is the sum of the time complexities of each individual call, which is O(d_max).

  8. The overall time complexity of the entire C program is also O(d_max).





Comment

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