The Program in C++ Program to check whether a number is a Duck Number or not is given below:
#include <iostream>
#include <string>
using namespace std;
bool isDuckNumber(int n) {
string str = to_string(n);
return str.find("0") != string::npos && str.find("0", str.find("0") + 1) != string::npos;
}
int main() {
int n;
cout << "Hello codeauri Family,Enter a number here to check if it is a duck number or not!:\n ";
cin >> n;
if (isDuckNumber(n)) {
cout << n << " is a Duck Number." << endl;
} else {
cout << n << " is not a Duck Number." << endl;
}
return 0;
}
Output:
Hello codeauri Family,Enter a number here to check if it is a duck number or not!:
70709
70709 is a Duck Number.
Pro-Tips💡
This program implements a function isDuckNumber
to check if a number is a Duck Number.
A Duck Number is a number that has at least two zeros in it.
The isDuckNumber
function converts n
to a string, then uses the find
method to search for the first zero and the second zero in the string.
If the find
method returns a position other than string::npos
, which indicates that the character was found, and the second find
method also returns a position other than string::npos
, the function returns true, indicating that the number is a Duck Number.
If either of the find
methods returns string::npos
, the function returns false, indicating that the number is not a Duck Number.
The main
function prompts the user to enter a number and calls the isDuckNumber
function with the input.
If the function returns true, the number is a Duck Number and the program prints a message to that effect.
If the function returns false, the number is not a Duck Number and the program prints a message to that effect.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.