JavaScript Program to Check Whether a Number is a Duck Number Last Updated : 15 Mar, 2024 Comments Improve Suggest changes Like Article Like Report Duck numbers are special numbers that occur when a number has the digit '0' in it, but the '0' is not the first digit. numbers require the digit '0' to appear in the number, but not as the first digit. JavaScript provided several methods to check whether a number is a duck number which are as follows: Table of Content Iterative Approach Recursive Method to check duck numberIterative ApproachThis method first converts the number into a string to check whether that number is a duck number or not. It simply skips zeros at the start of the string, if there are any. This code checks if the input, after handling leading zeros, contains the digit '0'. If 0 is present in that string, it is consider as a Duck Number, otherwise, it is not. Example: Duck Number Detection in JavaScript involves checking whether a given number is a "duck number" by ignoring leading zeros and then searching for the presence of a zero digit. JavaScript function duckNumber(input) { let i = 0, length = input.length; for (i; i < length && input[i] == '0'; i++) { // Loop to ignore leading 0s } // Check remaining digits for (i; i < length; i++) { if (input[i] == '0') { return true; } } return false; } // Example let num = "0123"; if (duckNumber(num)) console.log("It is a duck number"); else console.log("It is not a duck number"); OutputIt is not a duck number Time Complexity: O(n) Space Complexity: O(1) Recursive Method to check duck numberThis method first convert the number into string to check weather that number is duck number or not. It simply skips zeros at the start of the string, if there are any. It uses a recursive approach to traverse through the digits, printing whether the number is a Duck Number or not. Example: To determining Duck numbers in JavaScript by ignoring leading zeros and then checking for the presence of a zero digit. JavaScript function isDuckNumber(num, index = 1) { let str = num.toString(); if (index === str.length) { console .log(`${num} is not a Duck Number.`); return; } if (str[index] === '0') { console .log(`${num} is a Duck Number.`); return; } isDuckNumber(num, index + 1); } // Example const num = 23; isDuckNumber(num); Output23 is not a Duck Number. Time Complexity: O(n) Space Complexity: O(n) Comment More infoAdvertise with us Next Article JavaScript Program to Check Whether a Number is a Duck Number A abhinavsrivastava1920 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Check Whether a Number is Harshad Number A Harshad number (also called Niven number) is a number that is divisible by the sum of its digits. In other words, if you take a number, sum up its digits, and if the original number is divisible by that sum, then it's a Harshad number. For example, 18 is a Harshad number because the sum of its dig 2 min read JavaScript Program to Check Whether a Number is an Automorphic Number Numbers, whose square ends with the same digits as the number itself are referred to as automorphic numbers, sometimes known as circular or circular-permuted numbers. Examples:Input: number = 5Output: YesExplanation: 5 is a automorphic number, because the square of 5 is 25 which ends with 5 the digi 3 min read JavaScript Program to Check Whether a Number is Perfect Square The number that results from squaring another integer is called a perfect square. A perfect square is a number that can be expressed as the product of an integer with itself. There are several ways available in JavaScript to check whether a number is a perfect square or not which are as follows: Tab 4 min read JavaScript Program to Check for Palindrome Number We are going to learn about Palindrome Numbers in JavaScript. A palindrome number is a numerical sequence that reads the same forwards and backward, It remains unchanged even when reversed, retaining its original identity. Example: Input : Number = 121Output : PalindromeInput : Number = 1331Output : 4 min read JavaScript Program to Check whether the Input Number is a Neon Number A neon number is a number where the sum of digits of its square equals the number itself. For example, 9 is a neon number because 9^2 = 81, and the sum of the digits 81 is 9. There are several ways to check if a given number is a neon number in JavaScript or not which are as follows: Table of Conten 2 min read Like