C++ Program To Check Duck Numbers
A Duck number is a positive number which has zeroes present in it.Example: 120, 1024, 102030 are all Duck numbers.
Note : A Numbers with only leading 0s is not considered as Duck Number.
Example:
Numbers Like 007 or 001 are Not Considered as Duck Numbers.
To check if a number is a Duck Number or not in C++
you can use the following algorithm:
- Read the input number.
- Initialize a variable count to 0.
- While num is not equal to 0, perform the following steps:
- Get the last digit of the num by using the modulo operator (%).
- if last digit = 0 then increase count by 1
- Divide the num by 10 to remove the last digit.
- If count = '0' the number is a Duck Number.
- If count not = '0' the number is Not a Duck Number.
Note that this implementation assumes that the input number is a positive integer. You may need to add additional checks for negative numbers or other cases depending on your requirements.
Here is an example implementation in C++Source Code
//C++ Program To Check Duck Numbers
#include<iostream.h>
#include<conio.h>
void main()
{
int num , Rd , count=0;
cout<<"Enter a Number To Check : ";
cin>>num;
while( num!=0 )
{
Rd = num % 10 ;
if( Rd==0 )
count++;
num = num / 10 ;
}
if( count>=1 )
cout<<"It is a Duck Number . ";
else
cout<<"It is Not a Duck Number . ";
getch();
}
Output
Sample Input :
Enter a Number To Check : 1024
Sample Output:
It is a Duck Number
Sample Input :
Enter a Number To Check : 123
Sample Output:
It is Not a Duck Number