鸭号是其中包含零的正数,例如3210、8050896、70709都是鸭号。请注意,只有前导0的数字不被视为鸭号。例如,像035或0012这样的数字不被视为鸭号。像01203这样的数字被认为是Duck,因为其中存在非前导0。
例子 :
Input : 707069
Output : It is a duck number.
Explanation: 707069 does not contains zeros at the beginning.
Input : 02364
Output : It is not a duck number.
Explanation: in 02364 there is a zero at the beginning of the number.
C++
// C++ Program to check whether
// a number is Duck Number or not.
#include
using namespace std;
// Function to check whether
// given number is duck number or not.
bool check_duck(string num)
{
// Ignore leading 0s
int i = 0, n = num.length();
while (i < n && num[i] == '0')
i++;
// Check remaining digits
while (i < n) {
if (num[i] == '0')
return true;
i++;
}
return false;
}
// Driver Method
int main(void)
{
string num = "1023";
if (check_duck(num))
cout << "It is a duck number\n";
else
cout << "It is not a duck number\n";
return 0;
}
Java
// Java Program to check whether a
// number is Duck Number or not.
import java.io.*;
class GFG {
// Function to check whether
// the given number is duck number or not.
static boolean check_duck(String num)
{
// Ignore leading 0s
int i = 0, n = num.length();
while (i < n && num.charAt(i) == '0')
i++;
// Check remaining digits
while (i < n) {
if (num.charAt(i) == '0')
return true;
i++;
}
return false;
}
// Driver Method
public static void main(String args[]) throws IOException
{
String num = "1023";
if (check_duck(num))
System.out.println("It is a duck number");
else
System.out.println("It is not a duck number");
}
}
Python
# Python program to check whether a number is Duck Number or not.
# Function to check whether
# the given number is duck number or not.
def check_duck(num) :
# Length of the number(number of digits)
n = len(num)
# Ignore leading 0s
i = 0
while (i < n and num[i] == '0') :
i = i + 1
# Check remaining digits
while (i < n) :
if (num[i] == "0") :
return True
i = i + 1
return False
# Driver Method
num1 = "1023"
if(check_duck(num1)) :
print "It is a duck number"
else :
print "It is not a duck number"
# Write Python3 code here
C#
// C# Program to check whether a
// number is Duck Number or not.
using System;
class GFG {
// Function to check whether
// the given number is duck
// number or not.
static bool check_duck( String num)
{
// Ignore leading 0s
int i = 0, n = num.Length;
while (i < n && num[i] == '0')
i++;
// Check remaining digits
while (i < n) {
if (num[i] == '0')
return true;
i++;
}
return false;
}
// Driver Method
public static void Main()
{
String num1 = "1023";
// checking number1
if( check_duck(num1))
Console.Write("It is a "
+ "duck number");
else
Console.Write("It is not "
+ "a duck number");
}
}
// This code is contributed by
// nitin mittal.
Javascript
输出 :
It is a duck number.