Check if given number is stable or unstable

Here given code implementation process.

//C Program
//Check if given number is stable or unstable 
#include <stdio.h>

//Calculate the frequency of digits in number
void frequency(int number, int result[])
{
	int temp = 0;
	while (number != 0)
	{
		temp = number % 10;
		if (temp < 0)
		{
			//When get negative number
			temp = -temp;
		}
		//modify the value of temp index element in result array
		result[temp]++;
		number /= 10;
	}
}
void stable_no(int number)
{
	//resultant array which is store the frequency of number digits
	int result[10];
	for (int i = 0; i < 10; ++i)
	{
		//Set the initial value of each element to zero
		result[i] = 0;
	}
	int auxiliary = 0, status = 1;
	//Calculate number digit frequency
	frequency(number, result);
	for (int i = 0; i < 10 && status == 1; ++i)
	{
		//Check that the resultant array 
		//[i] location frequency is greater than zero or not
		if (result[i] != 0)
		{
			if (auxiliary == 0)
			{
				//get first number frequency
				auxiliary = result[i];
			}
			else if (auxiliary != result[i])
			{
				//When no two numbers frequency are same in given number
				//here, break the loop execution
				status = 0;
			}
		}
	}
	if (status == 0)
	{
		printf("%d Is not a stable number\n", number);
	}
	else
	{
		printf("%d Is an stable number\n", number);
	}
	printf("\n");
}
int main()
{
	//Test Case
	stable_no(123);
	stable_no(1111);
	stable_no(89198);
	stable_no(112233);
	stable_no(10101);
	return 0;
}

Output

123 Is an stable number

1111 Is an stable number

89198 Is not a stable number

112233 Is an stable number

10101 Is not a stable number
/*
  C++ Program
  Check if given number is stable or unstable 
*/
#include<iostream>

using namespace std;
class MyNumber
{
	public:
		//Calculate the frequency of digits in number
		void frequency(int number, int *result)
		{
			int temp = 0;
			while (number != 0)
			{
				temp = number % 10;
				if (temp < 0)
				{
					//When get negative number
					temp = -temp;
				}
				//modify the value of temp index element in result array
				result[temp]++;
				number /= 10;
			}
		}
	void stable_no(int number)
	{
		int *result = new int[10];
		for (int i = 0; i < 10; ++i)
		{
			//Set the initial value of each element to zero
			result[i] = 0;
		}
		int auxiliary = 0, status = 1;
		//Calculate number digit frequency
		this->frequency(number, result);
		for (int i = 0; i < 10 && status == 1; ++i)
		{
			//Check that the resultant array 
			//[i] location frequency is greater than zero or not
			if (result[i] != 0)
			{
				if (auxiliary == 0)
				{
					//get first number frequency
					auxiliary = result[i];
				}
				else if (auxiliary != result[i])
				{
					//When no two numbers frequency are same in given number
					//here, break the loop execution
					status = 0;
				}
			}
		}
		if (status == 0)
		{
			cout << "" << number << " Is not a stable number\n";
		}
		else
		{
			cout << "" << number << " Is an stable number\n";
		}
		cout << "\n";
	}
};
int main()
{
	MyNumber obj =  MyNumber();
	//Test Case
	obj.stable_no(123);
	obj.stable_no(1111);
	obj.stable_no(89198);
	obj.stable_no(112233);
	obj.stable_no(10101);
	return 0;
}

Output

123 Is an stable number

1111 Is an stable number

89198 Is not a stable number

112233 Is an stable number

10101 Is not a stable number
/*
  C# Program
  Check if given number is stable or unstable 
*/
using System;
public class MyNumber
{
	//Calculate the frequency of digits in number
	public void frequency(int number, int[] result)
	{
		int temp = 0;
		while (number != 0)
		{
			temp = number % 10;
			if (temp < 0)
			{
				//When get negative number
				temp = -temp;
			}
			//modify the value of temp index element in result array
			result[temp]++;
			number /= 10;
		}
	}
	public void stable_no(int number)
	{
      	//resultant array which is store the frequency of number digits
		int[] result = new int[10];
		for (int i = 0; i < 10; i++)
		{
			//Set the initial value of each element to zero
			result[i] = 0;
		}
		int auxiliary = 0, status = 1;
		//Calculate number digit frequency
		frequency(number, result);
		for (int i = 0; i < 10 && status == 1; i++)
		{
			//Check that the resultant array 
			//[i] location frequency is greater than zero or not
			if (result[i] != 0)
			{
				if (auxiliary == 0)
				{
					//get first number frequency
					auxiliary = result[i];
				}
				else if (auxiliary != result[i])
				{
					//When no two numbers frequency are same in given number
					//here, break the loop execution
					status = 0;
				}
			}
		}
		if (status == 0)
		{
			Console.Write("" + number + " Is not a stable number\n");
		}
		else
		{
			Console.Write("" + number + " Is an stable number\n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Case
		obj.stable_no(123);
		obj.stable_no(1111);
		obj.stable_no(89198);
		obj.stable_no(112233);
		obj.stable_no(10101);
	}
}

Output

123 Is an stable number

1111 Is an stable number

89198 Is not a stable number

112233 Is an stable number

10101 Is not a stable number
/*
  Java Program
  Check if given number is stable or unstable 
*/
public class MyNumber
{
	//Calculate the frequency of digits in number
	public void frequency(int number, int[] result)
	{
		int temp = 0;
		while (number != 0)
		{
			temp = number % 10;
			if (temp < 0)
			{
				//When get negative number
				temp = -temp;
			}
			//modify the value of temp index element in result array
			result[temp]++;
			number /= 10;
		}
	}
	public void stable_no(int number)
	{
		//resultant array which is store the frequency of number digits
		int[] result = new int[10];
		for (int i = 0; i < 10; ++i)
		{
			//Set the initial value of each element to zero
			result[i] = 0;
		}
		int auxiliary = 0, status = 1;
		//Calculate number digit frequency
		frequency(number, result);
		for (int i = 0; i < 10 && status == 1; ++i)
		{
			//Check that the resultant array 
			//[i] location frequency is greater than zero or not
			if (result[i] != 0)
			{
				if (auxiliary == 0)
				{
					//get first number frequency
					auxiliary = result[i];
				}
				else if (auxiliary != result[i])
				{
					//When no two numbers frequency are same in given number
					//here, break the loop execution
					status = 0;
				}
			}
		}
		if (status == 0)
		{
			System.out.print("" + number + " Is not a stable number\n");
		}
		else
		{
			System.out.print("" + number + " Is an stable number\n");
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Case
		obj.stable_no(123);
		obj.stable_no(1111);
		obj.stable_no(89198);
		obj.stable_no(112233);
		obj.stable_no(10101);
	}
}

Output

123 Is an stable number

1111 Is an stable number

89198 Is not a stable number

112233 Is an stable number

10101 Is not a stable number
<?php
/*
  Php Program
  Check if given number is stable or unstable 
*/
class MyNumber
{
	//Calculate the frequency of digits in number
	public 	function frequency($number, & $result)
	{
		$temp = 0;
		while ($number != 0)
		{
			$temp = $number % 10;
			if ($temp < 0)
			{
				//When get negative number
				$temp = -$temp;
			}
			//modify the value of temp index element in result array
			$result[$temp]++;
			$number = intval($number / 10);
		}
	}
	public 	function stable_no($number)
	{
		//resultant array which is store the frequency of number digits
		//Set the initial value of each element to zero
		$result = array_fill(0, 10, 0);
		$auxiliary = 0;
		$status = 1;
		//Calculate number digit frequency
		$this->frequency($number, $result);
		for ($i = 0; $i < 10 && $status == 1; ++$i)
		{
			//Check that the resultant array 
			//[i] location frequency is greater than zero or not
			if ($result[$i] != 0)
			{
				if ($auxiliary == 0)
				{
					//get first number frequency
					$auxiliary = $result[$i];
				}
				else if ($auxiliary != $result[$i])
				{
					//When no two numbers frequency are same in given number
					//here, break the loop execution
					$status = 0;
				}
			}
		}
		if ($status == 0)
		{
			echo("". $number ." Is not a stable number\n");
		}
		else
		{
			echo("". $number ." Is an stable number\n");
		}
		echo("\n");
	}
}

function main()
{
	$obj = new MyNumber();
	//Test Case
	$obj->stable_no(123);
	$obj->stable_no(1111);
	$obj->stable_no(89198);
	$obj->stable_no(112233);
	$obj->stable_no(10101);
}
main();

Output

123 Is an stable number

1111 Is an stable number

89198 Is not a stable number

112233 Is an stable number

10101 Is not a stable number
/*
  Node Js Program
  Check if given number is stable or unstable 
*/
class MyNumber
{
	//Calculate the frequency of digits in number
	frequency(number, result)
	{
		var temp = 0;
		while (number != 0)
		{
			temp = number % 10;
			if (temp < 0)
			{
				//When get negative number
				temp = -temp;
			}
			//modify the value of temp index element in result array
			result[temp]++;
			number = parseInt(number / 10);
		}
	}
	stable_no(number)
	{
		//resultant array which is store the frequency of number digits
		//Set the initial value of each element to zero
		var result = Array(10).fill(0);
		var auxiliary = 0;
		var status = 1;
		//Calculate number digit frequency
		this.frequency(number, result);
		for (var i = 0; i < 10 && status == 1; ++i)
		{
			//Check that the resultant array 
			//[i] location frequency is greater than zero or not
			if (result[i] != 0)
			{
				if (auxiliary == 0)
				{
					//get first number frequency
					auxiliary = result[i];
				}
				else
				if (auxiliary != result[i])
				{
					//When no two numbers frequency are same in given number
					//here, break the loop execution
					status = 0;
				}
			}
		}
		if (status == 0)
		{
			process.stdout.write("" + number + " Is not a stable number\n");
		}
		else
		{
			process.stdout.write("" + number + " Is an stable number\n");
		}
		process.stdout.write("\n");
	}
}

function main(args)
{
	var obj = new MyNumber();
	//Test Case
	obj.stable_no(123);
	obj.stable_no(1111);
	obj.stable_no(89198);
	obj.stable_no(112233);
	obj.stable_no(10101);
}
main();

Output

123 Is an stable number

1111 Is an stable number

89198 Is not a stable number

112233 Is an stable number

10101 Is not a stable number
#   Python 3 Program
#   Check if given number is stable or unstable 

class MyNumber:
	# Calculate the frequency of digits in number
	def frequency(self, number, result):
		temp = 0
		while (number != 0):
			temp = number % 10
			if (temp < 0):
				# When get negative number
				temp = -temp
			
			# modify the value of temp index element in result array
			result[temp] += 1
			number = int(number / 10)
		
	
	def stable_no(self, number):
		# Set the initial value of each element to zero
		result = [0] * 10
		i = 0
		auxiliary = 0
		status = 1
		# Calculate number digit frequency
		self.frequency(number, result)
		while (i < 10 and status == 1):
			# Check that the resultant array 
			# [i] location frequency is greater than zero or not
			if (result[i] != 0):
				if (auxiliary == 0):
					# get first number frequency
					auxiliary = result[i]
				
				elif (auxiliary != result[i]):
					# When no two numbers frequency are same in given number
					# here, break the loop execution
					status = 0
				
			
			i += 1
		
		if (status == 0):
			print("", number ," Is not a stable number\n", end = "")
		
		else:
			print("", number ," Is an stable number\n", end = "")
		
		print("\n", end = "")
	

def main():
	obj = MyNumber()
	# Test Case
	obj.stable_no(123)
	obj.stable_no(1111)
	obj.stable_no(89198)
	obj.stable_no(112233)
	obj.stable_no(10101)


if __name__ == "__main__": main()

Output

 123  Is an stable number

 1111  Is an stable number

 89198  Is not a stable number

 112233  Is an stable number

 10101  Is not a stable number
#   Ruby Program
#   Check if given number is stable or unstable 

class MyNumber

	# Calculate the frequency of digits in number
	def frequency(number, result)
	
		temp = 0
		while (number != 0)
		
			temp = number % 10
			if (temp < 0)
			
				# When get negative number
				temp = -temp
			end
			# modify the value of temp index element in result array
			result[temp] += 1
			number /= 10
		end
	end
	def stable_no(number)
	
		# Set the initial value of each element to zero
		result = Array.new(10) {0}
		i = 0
		auxiliary = 0
		status = 1
		# Calculate number digit frequency
		self.frequency(number, result)
		while (i < 10 && status == 1)
		
			# Check that the resultant array 
			# [i] location frequency is greater than zero or not
			if (result[i] != 0)
			
				if (auxiliary == 0)
				
					# get first number frequency
					auxiliary = result[i]
				elsif (auxiliary != result[i])
				
					# When no two numbers frequency are same in given number
					# here, break the loop execution
					status = 0
				end
			end
			i += 1
		end
		if (status == 0)
		
			print("", number ," Is not a stable number\n")
		else
		
			print("", number ," Is an stable number\n")
		end
		print("\n")
	end
end
def main()

	obj = MyNumber.new()
	# Test Case
	obj.stable_no(123)
	obj.stable_no(1111)
	obj.stable_no(89198)
	obj.stable_no(112233)
	obj.stable_no(10101)
end
main()

Output

123 Is an stable number

1111 Is an stable number

89198 Is not a stable number

112233 Is an stable number

10101 Is not a stable number

/*
  Scala Program
  Check if given number is stable or unstable 
*/
class MyNumber
{
	//Calculate the frequency of digits in number
	def frequency(num : Int, result: Array[Int]): Unit = {
		var temp: Int = 0;
      	var number: Int = num;
		while (number != 0)
		{
			temp = number % 10;
			if (temp < 0)
			{
				//When get negative number
				temp = -temp;
			}
			//modify the value of temp index element in result array
			result(temp) += 1;
			number = (number / 10).toInt;
		}
	}
	def stable_no(number: Int): Unit = {
		//Set the initial value of each element to zero
		var result: Array[Int] = Array.fill[Int](10)(0);
		var i: Int = 0;
		var auxiliary: Int = 0;
		var status: Int = 1;
		//Calculate number digit frequency
		frequency(number, result);
		while (i < 10 && status == 1)
		{
			//Check that the resultant array 
			//[i] location frequency is greater than zero or not
			if (result(i) != 0)
			{
				if (auxiliary == 0)
				{
					//get first number frequency
					auxiliary = result(i);
				}
				else if (auxiliary != result(i))
				{
					//When no two numbers frequency are same in given number
					//here, break the loop execution
					status = 0;
				}
			}
			i += 1;
		}
		if (status == 0)
		{
			print("" + number + " Is not a stable number\n");
		}
		else
		{
			print("" + number + " Is an stable number\n");
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.stable_no(123);
		obj.stable_no(1111);
		obj.stable_no(89198);
		obj.stable_no(112233);
		obj.stable_no(10101);
	}
}

Output

123 Is an stable number

1111 Is an stable number

89198 Is not a stable number

112233 Is an stable number

10101 Is not a stable number
/*
  Swift Program
  Check if given number is stable or unstable 
*/
class MyNumber
{
	//Calculate the frequency of digits in number
	func frequency(_ num:  Int, _ result: inout [Int])
	{
		var temp: Int = 0;
     	var number: Int = num;
		while (number != 0)
		{
			temp = number % 10;
			if (temp < 0)
			{
				//When get negative number
				temp = -temp;
			}
			//modify the value of temp index element in result array
			result[temp] += 1;
			number /= 10;
		}
	}
	func stable_no(_ number: Int)
	{
		//Set the initial value of each element to zero
		var result: [Int] = Array(repeating: 0, count: 10);
		var i: Int = 0;
		var auxiliary: Int = 0;
		var status: Int = 1;
		//Calculate number digit frequency
		self.frequency(number, &result);
		while (i < 10 && status == 1)
		{
			//Check that the resultant array 
			//[i] location frequency is greater than zero or not
			if (result[i] != 0)
			{
				if (auxiliary == 0)
				{
					//get first number frequency
					auxiliary = result[i];
				}
				else
				if (auxiliary != result[i])
				{
					//When no two numbers frequency are same in given number
					//here, break the loop execution
					status = 0;
				}
			}
			i += 1;
		}
		if (status == 0)
		{
			print("", number ," Is not a stable number\n", terminator: "");
		}
		else
		{
			print("", number ," Is an stable number\n", terminator: "");
		}
		print("\n", terminator: "");
	}
}
func main()
{
	let obj: MyNumber = MyNumber();
	//Test Case
	obj.stable_no(123);
	obj.stable_no(1111);
	obj.stable_no(89198);
	obj.stable_no(112233);
	obj.stable_no(10101);
}
main();

Output

 123  Is an stable number

 1111  Is an stable number

 89198  Is not a stable number

 112233  Is an stable number

 10101  Is not a stable number


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







© 2021, kalkicode.com, All rights reserved