Basic Programming C++ Examples

C++ Program to check whether a number is a Duck Number or not

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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X