w3resource

C++ Exercises: Find Duck Numbers between 1 to 500

C++ Numbers: Exercise-27 with Solution

Write a program in C++ to find Duck Numbers between 1 to 500.

Pictorial Presentation:

C++ Exercises: Find Duck Numbers between 1 to 500

Sample Solution:

C++ Code :

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int dno,dkno,r,flg;
	flg=0;
 cout << "\n\n Find Duck Numbers between 1 to 500: \n";
 cout << " ----------------------------------------\n";
 cout << " The Duck numbers are: "<<endl;
for(dkno=1;dkno<=500;dkno++)
	{
	dno=dkno;
	flg=0;
    while(dno>0)
        {
            if(dno % 10==0)
            {
            flg=1;
            break;

            }
			dno/=10;
        }
            if(dkno>0 && flg==1)
            {
            cout << dkno<<" ";
            }
	}
	cout<<endl;
}

Sample Output:

 Find Duck Numbers between 1 to 500:                                   
 ----------------------------------------                              
 The Duck numbers are:                                                 
10 20 30 40 50 60 70 80 90 100 101 102 103 104 105 106 107 108 109 110 
120 130 140 150 160 170 180 190 200 201 202 203 204 205 206 207 208 209
 210 220 230 240 250 260 270 280 290 300 301 302 303 304 305 306 307 30
8 309 310 320 330 340 350 360 370 380 390 400 401 402 403 404 405 406 4
07 408 409 410 420 430 440 450 460 470 480 490 500

Flowchart:

Flowchart: Find Duck Numbers between 1 to 500

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to check whether a number is a Duck Number or not.
Next: Write a program in C++ to check two numbers are Amicable numbers or not.

What is the difficulty level of this exercise?



Share this Tutorial / Exercise on : Facebook and Twitter

C++ Programming: Tips of the Day

How to use the PI constant in C++?

On some (especially older) platforms (see the comments below) you might need to

#define _USE_MATH_DEFINES and then include the necessary header file:
#include<math.h>

and the value of pi can be accessed via:

M_PI

In math.h (2014) it is defined as:

# define M_PI           3.14159265358979323846  /* pi */

but check your math.h for more. An extract from the "old" math.h (in 2009):

/* Define _USE_MATH_DEFINES before including math.h to expose these macro
 * definitions for common math constants.  These are placed under an #ifdef 
 * since these commonly-defined names are not part of the C/C++ standards.
 */

However:

  1. on newer platforms (at least on my 64 bit Ubuntu 14.04) I do not need to define the _USE_MATH_DEFINES
  2. On (recent) Linux platforms there are long double values too provided as a GNU Extension:
# define M_PIl          3.14159265358979323846

Ref: https://bit.ly/3G4BgzQ