﻿ C++ Exercises: Find the Abundant numbers between 1 to 1000 - w3resource

# C++ Exercises: Find the Abundant numbers between 1 to 1000

## C++ Numbers: Exercise-3 with Solution

Write a program in C++ to find the Abundant numbers (integers) between 1 to 1000.

Pictorial Presentation:

Sample Solution:

C++ Code :

``````#include <bits/stdc++.h>
using namespace std;
int getSum(int n)
{
int sum = 0;
for (int i=1; i<=sqrt(n); i++)
{
if (n%i==0)
{
if (n/i == i)
sum = sum + i;
else
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
sum = sum - n;
return sum;
}
bool checkAbundant(int n)
{
return (getSum(n) > n);
}
int main()
{
int n;
cout << "\n\n The Abundant number between 1 to 1000 are: \n";
cout << " -----------------------------------------------\n";
for(int j=1;j<=1000;j++)
{
n=j;
checkAbundant(n)? cout << n<<" ": cout << "";
}
cout << endl;
}
``````

Sample Output:

``` The Abundant number between 1 to 1000 are:
-----------------------------------------------
12 18 20 24 30 36 40 42 48 54 56 60 66 70 72 78 80 84 88 90 96 100 102 104 108 112 114 120 126 132 13
8 140 144 150 156 160 162 168 174 176 180 186 192 196 198 200 204 208 210 216 220 222 224 228 234 240
246 252 258 260 264 270 272 276 280 282 288 294 300 304 306 308 312 318 320 324 330 336 340 342 348
350 352 354 360 364 366 368 372 378 380 384 390 392 396 400 402 408 414 416 420 426 432 438 440 444 4
48 450 456 460 462 464 468 474 476 480 486 490 492 498 500 504 510 516 520 522 528 532 534 540 544 54
6 550 552 558 560 564 570 572 576 580 582 588 594 600 606 608 612 616 618 620 624 630 636 640 642 644
648 650 654 660 666 672 678 680 684 690 696 700 702 704 708 714 720 726 728 732 736 738 740 744 748
750 756 760 762 768 770 774 780 784 786 792 798 800 804 810 812 816 820 822 828 832 834 836 840 846 8
52 858 860 864 868 870 876 880 882 888 894 896 900 906 910 912 918 920 924 928 930 936 940 942 945 94
8 952 954 960 966 968 972 978 980 984 990 992 996 1000
```

Flowchart:

C++ Code Editor:

What is the difficulty level of this exercise?

﻿

## 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