Check whether a number is duck number or not

Here given code implementation process.

// C Program 
// Check Whether a number is duck number or not
#include <stdio.h>

// Determine whether given number is duck number or not
void is_duck_number(char str_num[], int size)
{
	if (size <= 0)
	{
		//When given string number is empty
		return;
	}
	int status = 0;
	int counter = 0;
	//This is used to check valid positive number
	int wrong = 0;
	// Skip the all beginning zeros
	while (counter < size && str_num[counter] == '0')
	{
		counter++;
	}
	while (counter < size && wrong == 0)
	{
		if (str_num[counter] >= '0' && str_num[counter] <= '9')
		{
			if (str_num[counter] == '0')
			{
				//We get zero
				status = 1;
				//Terminate loop when given number always valid number
			}
		}
		else
		{
			//When not a valid digit
			wrong = 1;
		}
		counter++;
	}
	if (wrong == 0 && status == 1)
	{
		printf("\n [%s] is duck number", str_num);
	}
	else
	{
		printf("\n [%s] is not duck number", str_num);
	}
}
int main()
{
	//Test string numbers
	char str_num1[] = "529043043";
	char str_num2[] = "0001234";
	char str_num3[] = "00012304";
	char str_num4[] = "1000000";
	char str_num5[] = "-1000000";
	char str_num6[] = "2300200100";
	int size = 0;
	//Test case 1
	size = sizeof(str_num1) / sizeof(str_num1[0]) - 1;
	is_duck_number(str_num1, size);
	//Test case 2
	size = sizeof(str_num2) / sizeof(str_num2[0]) - 1;
	is_duck_number(str_num2, size);
	//Test case 3
	size = sizeof(str_num3) / sizeof(str_num3[0]) - 1;
	is_duck_number(str_num3, size);
	//Test case 4
	size = sizeof(str_num4) / sizeof(str_num4[0]) - 1;
	is_duck_number(str_num4, size);
	//Test case 5
	size = sizeof(str_num5) / sizeof(str_num5[0]) - 1;
	is_duck_number(str_num5, size);
	//Test case 6
	size = sizeof(str_num6) / sizeof(str_num6[0]) - 1;
	is_duck_number(str_num6, size);
	return 0;
}

Output

 [529043043] is duck number
 [0001234] is not duck number
 [00012304] is duck number
 [1000000] is duck number
 [-1000000] is not duck number
 [2300200100] is duck number
/* 
  Java program 
  Check Whether a number is duck number or not
*/
class DuckNumber
{
	// Determine whether given number is duck number or not
	public void is_duck_number(String str_num, int size)
	{
		if (size <= 0)
		{
			//When given string number is empty
			return;
		}
		int counter = 0;
		//This is used to check valid positive number
		boolean wrong = false;
		boolean status = false;
		// Skip the all beginning zeros
		while (counter < size && str_num.charAt(counter) == '0')
		{
			counter++;
		}
		while (counter < size && wrong == false)
		{
			if (str_num.charAt(counter) >= '0' && str_num.charAt(counter) <= '9')
			{
				//Terminate loop when given number always valid number
				if (str_num.charAt(counter) == '0')
				{
					//We get zero
					status = true;
				}
			}
			else
			{
				//When not a valid digit
				wrong = true;
			}
			counter++;
		}
		if (wrong == false && status == true)
		{
			System.out.print("\n [" + str_num + "] is duck number");
		}
		else
		{
			System.out.print("\n [" + str_num + "] is not duck number");
		}
	}
	public static void main(String[] args)
	{
		DuckNumber obj = new DuckNumber();
		//Test string numbers
		String str_num1 = "529043043";
		String str_num2 = "0001234";
		String str_num3 = "00012304";
		String str_num4 = "1000000";
		String str_num5 = "-1000000";
		String str_num6 = "2300200100";
		int size = 0;
		//Test case 1
		size = str_num1.length();
		obj.is_duck_number(str_num1, size);
		//Test case 2
		size = str_num2.length();
		obj.is_duck_number(str_num2, size);
		//Test case 3
		size = str_num3.length();
		obj.is_duck_number(str_num3, size);
		//Test case 4
		size = str_num4.length();
		obj.is_duck_number(str_num4, size);
		//Test case 5
		size = str_num5.length();
		obj.is_duck_number(str_num5, size);
		//Test case 6
		size = str_num6.length();
		obj.is_duck_number(str_num6, size);
	}
}

Output

 [529043043] is duck number
 [0001234] is not duck number
 [00012304] is duck number
 [1000000] is duck number
 [-1000000] is not duck number
 [2300200100] is duck number
//Include header file
#include <iostream>
#include<string.h>

using namespace std;
/*
  C++ program 
  Check Whether a number is duck number or not
*/
class DuckNumber
{
	public:
		// Determine whether given number is duck number or not
		void is_duck_number(string str_num, int size)
		{
			if (size <= 0)
			{
				//When given string number is empty
				return;
			}
			int counter = 0;
			//This is used to check valid positive number
			bool wrong = false;
			bool status = false;
			// Skip the all beginning zeros
			while (counter < size && str_num[counter] == '0')
			{
				counter++;
			}
			while (counter < size && wrong == false)
			{
				if (str_num[counter] >= '0' && str_num[counter] <= '9')
				{
					//Terminate loop when given number always valid number
					if (str_num[counter] == '0')
					{
						//We get zero
						status = true;
					}
				}
				else
				{
					//When not a valid digit
					wrong = true;
				}
				counter++;
			}
			if (wrong == false && status == true)
			{
				cout << "\n [" << str_num << "] is duck number";
			}
			else
			{
				cout << "\n [" << str_num << "] is not duck number";
			}
		}
};
int main()
{
	DuckNumber obj = DuckNumber();
	//Test string numbers
	string str_num1 = "529043043";
	string str_num2 = "0001234";
	string str_num3 = "00012304";
	string str_num4 = "1000000";
	string str_num5 = "-1000000";
	string str_num6 = "2300200100";
	int size = 0;
	//Test case 1
	size = str_num1.size();
	obj.is_duck_number(str_num1, size);
	//Test case 2
	size = str_num2.size();
	obj.is_duck_number(str_num2, size);
	//Test case 3
	size = str_num3.size();
	obj.is_duck_number(str_num3, size);
	//Test case 4
	size = str_num4.size();
	obj.is_duck_number(str_num4, size);
	//Test case 5
	size = str_num5.size();
	obj.is_duck_number(str_num5, size);
	//Test case 6
	size = str_num6.size();
	obj.is_duck_number(str_num6, size);
	return 0;
}

Output

 [529043043] is duck number
 [0001234] is not duck number
 [00012304] is duck number
 [1000000] is duck number
 [-1000000] is not duck number
 [2300200100] is duck number
//Include namespace system
using System;

/* 
  C# program 
  Check Whether a number is duck number or not
*/

class DuckNumber
{
	// Determine whether given number is duck number or not
	public void is_duck_number(String str_num, int size)
	{
		if (size <= 0)
		{
			//When given string number is empty
			return;
		}
		int counter = 0;
		//This is used to check valid positive number
		Boolean wrong = false;
		Boolean status = false;
		// Skip the all beginning zeros
		while (counter < size && str_num[counter] == '0')
		{
			counter++;
		}
		while (counter < size && wrong == false)
		{
			if (str_num[counter] >= '0' && str_num[counter] <= '9')
			{
				//Terminate loop when given number always valid number
				if (str_num[counter] == '0')
				{
					//We get zero
					status = true;
				}
			}
			else
			{
				//When not a valid digit
				wrong = true;
			}
			counter++;
		}
		if (wrong == false && status == true)
		{
			Console.Write("\n [" + str_num + "] is duck number");
		}
		else
		{
			Console.Write("\n [" + str_num + "] is not duck number");
		}
	}
	public static void Main(String[] args)
	{
		DuckNumber obj = new DuckNumber();
		//Test string numbers
		String str_num1 = "529043043";
		String str_num2 = "0001234";
		String str_num3 = "00012304";
		String str_num4 = "1000000";
		String str_num5 = "-1000000";
		String str_num6 = "2300200100";
		int size = 0;
		//Test case 1
		size = str_num1.Length;
		obj.is_duck_number(str_num1, size);
		//Test case 2
		size = str_num2.Length;
		obj.is_duck_number(str_num2, size);
		//Test case 3
		size = str_num3.Length;
		obj.is_duck_number(str_num3, size);
		//Test case 4
		size = str_num4.Length;
		obj.is_duck_number(str_num4, size);
		//Test case 5
		size = str_num5.Length;
		obj.is_duck_number(str_num5, size);
		//Test case 6
		size = str_num6.Length;
		obj.is_duck_number(str_num6, size);
	}
}

Output

 [529043043] is duck number
 [0001234] is not duck number
 [00012304] is duck number
 [1000000] is duck number
 [-1000000] is not duck number
 [2300200100] is duck number
<?php
/* 
  Php program 
  Check Whether a number is duck number or not
*/
class DuckNumber
{
	// Determine whether given number is duck number or not
	public	function is_duck_number($str_num, $size)
	{
		if ($size <= 0)
		{
			//When given string number is empty
			return;
		}
		$counter = 0;
		//This is used to check valid positive number
		$wrong = false;
		$status = false;
		// Skip the all beginning zeros
		while ($counter < $size && $str_num[$counter] == '0')
		{
			$counter++;
		}
		while ($counter < $size && $wrong == false)
		{
			if (ord($str_num[$counter]) >= ord('0') && ord($str_num[$counter]) <= ord('9'))
			{
				//Terminate loop when given number always valid number
				if ($str_num[$counter] == '0')
				{
					//We get zero
					$status = true;
				}
			}
			else
			{
				//When not a valid digit
				$wrong = true;
			}
			$counter++;
		}
		if ($wrong == false && $status == true)
		{
			echo "\n [". $str_num ."] is duck number";
		}
		else
		{
			echo "\n [". $str_num ."] is not duck number";
		}
	}
}

function main()
{
	$obj = new DuckNumber();
	//Test string numbers
	$str_num1 = "529043043";
	$str_num2 = "0001234";
	$str_num3 = "00012304";
	$str_num4 = "1000000";
	$str_num5 = "-1000000";
	$str_num6 = "2300200100";
	$size = 0;
	//Test case 1
	$size = strlen($str_num1);
	$obj->is_duck_number($str_num1, $size);
	//Test case 2
	$size = strlen($str_num2);
	$obj->is_duck_number($str_num2, $size);
	//Test case 3
	$size = strlen($str_num3);
	$obj->is_duck_number($str_num3, $size);
	//Test case 4
	$size = strlen($str_num4);
	$obj->is_duck_number($str_num4, $size);
	//Test case 5
	$size = strlen($str_num5);
	$obj->is_duck_number($str_num5, $size);
	//Test case 6
	$size = strlen($str_num6);
	$obj->is_duck_number($str_num6, $size);
}
main();

Output

 [529043043] is duck number
 [0001234] is not duck number
 [00012304] is duck number
 [1000000] is duck number
 [-1000000] is not duck number
 [2300200100] is duck number
/* 
  Node Js program 
  Check Whether a number is duck number or not
*/
class DuckNumber
{
	// Determine whether given number is duck number or not
	is_duck_number(str_num, size)
	{
		if (size <= 0)
		{
			//When given string number is empty
			return;
		}
		var counter = 0;
		//This is used to check valid positive number
		var wrong = false;
		var status = false;
		// Skip the all beginning zeros
		while (counter < size && str_num[counter] == '0')
		{
			counter++;
		}
		while (counter < size && wrong == false)
		{
			if ((str_num[counter]).charCodeAt(0) >= ('0').charCodeAt(0) && (str_num[counter]).charCodeAt(0) <= ('9').charCodeAt(0))
			{
				//Terminate loop when given number always valid number
				if (str_num[counter] == '0')
				{
					//We get zero
					status = true;
				}
			}
			else
			{
				//When not a valid digit
				wrong = true;
			}
			counter++;
		}
		if (wrong == false && status == true)
		{
			process.stdout.write("\n [" + str_num + "] is duck number");
		}
		else
		{
			process.stdout.write("\n [" + str_num + "] is not duck number");
		}
	}
}

function main()
{
	var obj = new DuckNumber();
	//Test string numbers
	var str_num1 = "529043043";
	var str_num2 = "0001234";
	var str_num3 = "00012304";
	var str_num4 = "1000000";
	var str_num5 = "-1000000";
	var str_num6 = "2300200100";
	var size = 0;
	//Test case 1
	size = str_num1.length;
	obj.is_duck_number(str_num1, size);
	//Test case 2
	size = str_num2.length;
	obj.is_duck_number(str_num2, size);
	//Test case 3
	size = str_num3.length;
	obj.is_duck_number(str_num3, size);
	//Test case 4
	size = str_num4.length;
	obj.is_duck_number(str_num4, size);
	//Test case 5
	size = str_num5.length;
	obj.is_duck_number(str_num5, size);
	//Test case 6
	size = str_num6.length;
	obj.is_duck_number(str_num6, size);
}
main();

Output

 [529043043] is duck number
 [0001234] is not duck number
 [00012304] is duck number
 [1000000] is duck number
 [-1000000] is not duck number
 [2300200100] is duck number
#   Python 3 program 
#   Check Whether a number is duck number or not

class DuckNumber :
	#  Determine whether given number is duck number or not
	def is_duck_number(self, str_num, size) :
		if (size <= 0) :
			# When given string number is empty
			return
		
		counter = 0
		# This is used to check valid positive number
		wrong = False
		status = False
		#  Skip the all beginning zeros
		while (counter < size and str_num[counter] == '0') :
			counter += 1
		
		while (counter < size and wrong == False) :
			if (ord(str_num[counter]) >= ord('0') and ord(str_num[counter]) <= ord('9')) :
				# Terminate loop when given number always valid number
				if (str_num[counter] == '0') :
					# We get zero
					status = True
				
			else :
				# When not a valid digit
				wrong = True
			
			counter += 1
		
		if (wrong == False and status == True) :
			print("\n [", str_num ,"] is duck number", end = "")
		else :
			print("\n [", str_num ,"] is not duck number", end = "")
		
	

def main() :
	obj = DuckNumber()
	# Test string numbers
	str_num1 = "529043043"
	str_num2 = "0001234"
	str_num3 = "00012304"
	str_num4 = "1000000"
	str_num5 = "-1000000"
	str_num6 = "2300200100"
	size = 0
	# Test case 1
	size = len(str_num1)
	obj.is_duck_number(str_num1, size)
	# Test case 2
	size = len(str_num2)
	obj.is_duck_number(str_num2, size)
	# Test case 3
	size = len(str_num3)
	obj.is_duck_number(str_num3, size)
	# Test case 4
	size = len(str_num4)
	obj.is_duck_number(str_num4, size)
	# Test case 5
	size = len(str_num5)
	obj.is_duck_number(str_num5, size)
	# Test case 6
	size = len(str_num6)
	obj.is_duck_number(str_num6, size)

if __name__ == "__main__": main()

Output

 [ 529043043 ] is duck number
 [ 0001234 ] is not duck number
 [ 00012304 ] is duck number
 [ 1000000 ] is duck number
 [ -1000000 ] is not duck number
 [ 2300200100 ] is duck number
#   Ruby program 
#   Check Whether a number is duck number or not

class DuckNumber 
	#  Determine whether given number is duck number or not
	def is_duck_number(str_num, size) 
		if (size <= 0) 
			# When given string number is empty
			return
		end

		counter = 0
		# This is used to check valid positive number
		wrong = false
		status = false
		#  Skip the all beginning zeros
		while (counter < size && str_num[counter] == '0') 
			counter += 1
		end

		while (counter < size && wrong == false) 
			if ((str_num[counter]).ord >= ('0').ord && (str_num[counter]).ord <= ('9').ord) 
				# Terminate loop when given number always valid number
				if (str_num[counter] == '0') 
					# We get zero
					status = true
				end

			else 
				# When not a valid digit
				wrong = true
			end

			counter += 1
		end

		if (wrong == false && status == true) 
			print("\n [", str_num ,"] is duck number")
		else 
			print("\n [", str_num ,"] is not duck number")
		end

	end

end

def main() 
	obj = DuckNumber.new()
	# Test string numbers
	str_num1 = "529043043"
	str_num2 = "0001234"
	str_num3 = "00012304"
	str_num4 = "1000000"
	str_num5 = "-1000000"
	str_num6 = "2300200100"
	size = 0
	# Test case 1
	size = str_num1.length()
	obj.is_duck_number(str_num1, size)
	# Test case 2
	size = str_num2.length()
	obj.is_duck_number(str_num2, size)
	# Test case 3
	size = str_num3.length()
	obj.is_duck_number(str_num3, size)
	# Test case 4
	size = str_num4.length()
	obj.is_duck_number(str_num4, size)
	# Test case 5
	size = str_num5.length()
	obj.is_duck_number(str_num5, size)
	# Test case 6
	size = str_num6.length()
	obj.is_duck_number(str_num6, size)
end

main()

Output

 [529043043] is duck number
 [0001234] is not duck number
 [00012304] is duck number
 [1000000] is duck number
 [-1000000] is not duck number
 [2300200100] is duck number
/* 
  Scala program 
  Check Whether a number is duck number or not
*/
class DuckNumber
{
	// Determine whether given number is duck number or not
	def is_duck_number(str_num: String, size: Int): Unit = {
		if (size <= 0)
		{
			//When given string number is empty
			return;
		}
		var counter: Int = 0;
		//This is used to check valid positive number
		var wrong: Boolean = false;
		var status: Boolean = false;
		// Skip the all beginning zeros
		while (counter < size && str_num(counter) == '0')
		{
			counter += 1;
		}
		while (counter < size && wrong == false)
		{
			if (str_num(counter) >= '0' && str_num(counter) <= '9')
			{
				//Terminate loop when given number always valid number
				if (str_num(counter) == '0')
				{
					//We get zero
					status = true;
				}
			}
			else
			{
				//When not a valid digit
				wrong = true;
			}
			counter += 1;
		}
		if (wrong == false && status == true)
		{
			print("\n [" + str_num + "] is duck number");
		}
		else
		{
			print("\n [" + str_num + "] is not duck number");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: DuckNumber = new DuckNumber();
		//Test string numbers
		var str_num1: String = "529043043";
		var str_num2: String = "0001234";
		var str_num3: String = "00012304";
		var str_num4: String = "1000000";
		var str_num5: String = "-1000000";
		var str_num6: String = "2300200100";
		var size: Int = 0;
		//Test case 1
		size = str_num1.length();
		obj.is_duck_number(str_num1, size);
		//Test case 2
		size = str_num2.length();
		obj.is_duck_number(str_num2, size);
		//Test case 3
		size = str_num3.length();
		obj.is_duck_number(str_num3, size);
		//Test case 4
		size = str_num4.length();
		obj.is_duck_number(str_num4, size);
		//Test case 5
		size = str_num5.length();
		obj.is_duck_number(str_num5, size);
		//Test case 6
		size = str_num6.length();
		obj.is_duck_number(str_num6, size);
	}
}

Output

 [529043043] is duck number
 [0001234] is not duck number
 [00012304] is duck number
 [1000000] is duck number
 [-1000000] is not duck number
 [2300200100] is duck number
/* 
  Swift 4 program 
  Check Whether a number is duck number or not
*/
class DuckNumber
{
	// Determine whether given number is duck number or not
	func is_duck_number(_ text: String, _ size: Int)
	{
		if (size <= 0)
		{
			//When given string number is empty
			return;
		}
      	let str_num = Array(text);
		var counter: Int = 0;
		//This is used to check valid positive number
		var wrong: Bool = false;
		var status: Bool = false;
		// Skip the all beginning zeros
		while (counter < size && str_num[counter] == "0")
		{
			counter += 1;
		}
		while (counter < size && wrong == false)
		{
			if (str_num[counter] >= "0" && str_num[counter] <= "9")
			{
				//Terminate loop when given number always valid number
				if (str_num[counter] == "0")
				{
					//We get zero
					status = true;
				}
			}
			else
			{
				//When not a valid digit
				wrong = true;
			}
			counter += 1;
		}
		if (wrong == false && status == true)
		{
			print("\n [", text ,"] is duck number", terminator: "");
		}
		else
		{
			print("\n [", text ,"] is not duck number", terminator: "");
		}
	}
}
func main()
{
	let obj: DuckNumber = DuckNumber();
	//Test string numbers
	let str_num1: String = "529043043";
	let str_num2: String = "0001234";
	let str_num3: String = "00012304";
	let str_num4: String = "1000000";
	let str_num5: String = "-1000000";
	let str_num6: String = "2300200100";
	var size: Int = 0;
	//Test case 1
	size = str_num1.count;
	obj.is_duck_number(str_num1, size);
	//Test case 2
	size = str_num2.count;
	obj.is_duck_number(str_num2, size);
	//Test case 3
	size = str_num3.count;
	obj.is_duck_number(str_num3, size);
	//Test case 4
	size = str_num4.count;
	obj.is_duck_number(str_num4, size);
	//Test case 5
	size = str_num5.count;
	obj.is_duck_number(str_num5, size);
	//Test case 6
	size = str_num6.count;
	obj.is_duck_number(str_num6, size);
}
main();

Output

 [ 529043043 ] is duck number
 [ 0001234 ] is not duck number
 [ 00012304 ] is duck number
 [ 1000000 ] is duck number
 [ -1000000 ] is not duck number
 [ 2300200100 ] is duck 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