The Program in C++ Program to Find Duck Numbers between 1 to 1000 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() {
cout << "Hello Codeauri Family, The Duck Numbers between 1 and 1000 are: ";
for (int i = 1; i <= 1000; i++) {
if (isDuckNumber(i)) {
cout << i << " ";
}
}
cout << endl;
return 0;
}
Output:
Hello Codeauri Family, The Duck Numbers between 1 and 1000 are: 100 200 300 400 500 600 700 800 900 1000
Pro-Tips💡
This program is similar to the previous program, but instead of prompting the user for input, it loops through all numbers between 1 and 1000 and calls the isDuckNumber
function for each number.
If the function returns true, the number is a Duck Number and is printed.
The output is a list of all Duck Numbers between 1 and 1000.
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.