 # C++ Numbers: Exercises, Practice, Solution

## C++ Numbers [46 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a program in C++ to check whether a given number is an ugly number or not. Go to the editor
Sample output:
Input an integer number: 25
It is an ugly number.
Click me to see the sample solution

2. Write a program in C++ to check whether a given number is Abundant or not. Go to the editor
Sample Output:
Input an integer number: 35
The number is not Abundant.
Click me to see the sample solution

3. Write a program in C++ to find the Abundant numbers (integers) between 1 to 1000. Go to the editor
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 ```
Click me to see the sample solution

4. Write a program in C++ to check whether a given number is Perfect or not. Go to the editor
Sample Output:
Input a number: 28
28 is a perfect number
Click me to see the sample solution

5. Write a program in C++ to find Perfect numbers and number of Perfect numbers between 1 to 1000. Go to the editor
Sample Output:
The Perfect numbers are : 6 is a perfect number. 28 is a perfect number. 496 is a perfect number. Number of perfect numbers between 1 to 1000 is: 3
Click me to see the sample solution

6. Write a program in C++ to check whether a given number is Deficient or not. Go to the editor
Sample Output:
Input an integer number: 25
The number is Deficient.
Click me to see the sample solution

7. Write a program in C++ to find the Deficient numbers (integers) between 1 to 100. Go to the editor
Sample output:
The Deficient numbers between 1 to 100 are:
------------------------------------------------
1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 19 21 22 23 25 26 27 29 31 32 33 3 4 35 37 38 39 41 43 44 45 46 47 49 50 51 52 53 55 57 58 59 61 62 63 64 65 67 68 69 71 73 74 75 76 77 79 81 82 83 85 86 87 89 91 92 93 94 95 97 98 99
The Total number of Deficient numbers are: 76
Click me to see the sample solution

8. Write a program in C++ to generate random integers in a specific range. Go to the editor
Sample Output:
Input the lower range of number: 15
Input the upper range of number: 25
The random number between 15 and 25 is: 18
Click me to see the sample solution

9. Write a program in C++ to check whether a given number is a Kaprekar number or not. Go to the editor
Sample Output:
Input a number: 45.
45 is a Kaprekar number
Click me to see the sample solution

10. Write a program in C++ to generate and show all Kaprekar numbers less than 1000. Go to the editor
Sample Output:
The Kaprekar numbers less than 1000 are:
1 9 45 55 99 297 703 999
Click me to see the sample solution

11. Write a program in C++ to check whether a number is Lychrel number or not. Go to the editor
Sample Output:
Input a number: 196
The number is a Lychrel number.
Click me to see the sample solution

12. Write a program in C++ to find the Lychrel numbers and the number of Lychrel number within the range 1 to 1000(after 500 iteration). Go to the editor
Sample Output:
The Lychrel numbers are : 196 295 394 493 592 689 691 788 790 879 887 978 986
The number of Lychrel numbers are: 13
Click me to see the sample solution

13. Write a program in C++ to generate and show the first 15 Narcissistic decimal numbers. Go to the editor
Sample Output:
The first 15 narcissistic decimal numbers are:
1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208
Click me to see the sample solution

14. Write a program in C++ to display the first 10 Lucus numbers. Go to the editor
Sample Output:
The first 10 Lucus numbers are:
2 1 3 4 7 11 18 29 47 76
Click me to see the sample solution

15. Write a program in C++ to display the first 10 Catlan numbers. Go to the editor
Sample Output:
The first 10 Catlan numbers are:
1 1 2 5 14 42 132 429 1430 4862
Click me to see the sample solution

16. Write a program in C++ to check a number is a Happy or not. Go to the editor
Sample Output:
Input a number: 23
23 is a Happy number
Click me to see the sample solution

17. Write a program in C++ to find the Happy numbers between 1 to 1000. Go to the editor
Sample Output:
The happy numbers between 1 to 1000 are:
1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 103 109 129 130 133 139 167 176 188 190 19 2 193 203 208 219 226 230 236 239 262 263 280 291 293 301 302 310 313 319 320 326 329 331 338 356 362 365 367 368 376 379 383 386 391 392 397 404 409 440 446 464 469 478 487 490 496 536 556 563 565 566 608 617 622 623 632 635 637 638 644 649 653 655 656 665 671 673 680 683 694 700 709 716 736 739 748 761 763 784 790 793 802 806 818 820 833 836 847 860 863 874 881 888 899 901 904 907 910 912 913 921 923 931 932 937 940 946 964 970 973 989 998 1000
Click me to see the sample solution

18. Write a program in C++ to check whether a number is Disarium or not. Go to the editor
Sample Output:
Input a number: 9
The given number is a Disarium Number
Click me to see the sample solution

19. Write a program in C++ to find Disarium numbers between 1 to 1000. Go to the editor
Sample Output:
The Disarium numbers are:
1 2 3 4 5 6 7 8 9 89 135 175 518 598
Click me to see the sample solution

20. Write a program in C++ to check if a number is Harshad Number or not. Go to the editor
Sample Output:
Input a number: 18
The given number is a Harshad Number.
Click me to see the sample solution

21. Write a program in C++ to find Harshad Number between 1 to 100. Go to the editor
Sample Output:
Input number of terms: 5
The even numbers are: 2 4 6 8 10
The Sum of even Natural Numbers upto 5 terms: 30
Click me to see the sample solution

22. Write a program in C++ to check whether a number is a Pronic Number or Heteromecic Number or not. Go to the editor
1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Sample Output:
Input a number: 42
The given number is a Pronic Number
Click me to see the sample solution

23. Write a program in C++ to find Pronic Number between 1 to 1000. Go to the editor
Sample Output:
The Pronic numbers are:
2 6 12 20 30 42 56 72 90 110 132 156 182 210 240 272 306 342 380 420 462 506 552 600 650 702 756 812 870 930 992
Click me to see the sample solution

24. Write a program in C++ to check if a number is Authomorphic or not. Go to the editor
Sample Output:
Input a number: 25
The given number is an Automorphic Number.
Click me to see the sample solution

25. Write a program in C++ to find the Authomorphic numbers between 1 to 1000. Go to the editor
Sample Output:
The Authomorphic numbers are:
1 5 6 25 76 376 625
Click me to see the sample solution

26. Write a program in C++ to check whether a number is a Duck Number or not. Go to the editor
Sample Output:
Input a number: 30
The given number is a Duck Number.
Click me to see the sample solution

27. Write a program in C++ to find Duck Numbers between 1 to 500. Go to the editor
Sample Output:
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
Click me to see the sample solution

28. Write a program in C++ to check two numbers are Amicable numbers or not. Go to the editor
Sample Output:
Input the 1st number : 220 Input the 2nd number : 284 The given numbers are an Amicable pair.
Click me to see the sample solution

29. Write a program in C++ to count the amicable pairs in an array. Go to the editor
Sample Output:
Count the Amicable pairs in a specific array:
Sample pairs : (220, 284)(1184,1210) (2620,2924) (5020,5564) (6232,6368)...
------------------------------------------------------------------------------
Input the number of elements to be stored in the array: 2
element - 0: 220
element - 1: 284
Number of Amicable pairs presents in the array: 1
Click me to see the sample solution

30. Write a program in C++ to check if a given number is circular prime or not. Go to the editor
Sample Output:
Input a Number: 11
The given number is a circular prime Number
Click me to see the sample solution

31. Write a program in C++ to find circular prime numbers upto a specific limit. Go to the editor
Sample Output:
Enter the upper Limit: 248
The Circular Prime Numbers less than 248 are:
2 3 5 7 11 13 17 31 37 71 73 79 97 113 131 197 199
Click me to see the sample solution

32. Write a program in C++ to check whether a given number is a perfect cube or not. Go to the editor
Sample Output:
Input a number: 8
The number is a perfect Cube of 2
Click me to see the sample solution

33. Write a program in C++ to display first 10 Fermat numbers. Go to the editor
Sample Output:
The first 10 Fermat numbers are: 3 5 17 257 65537 4.29497e+09 1.84467e+19 3.40282e+38 1.15792e+77 1.34078e+154 inf
Click me to see the sample solution

34. Write a program in C++ to find any number between 1 and n that can be expressed as the sum of two cubes in two (or more) different ways. Go to the editor
Sample Output:
The numbers in the above range are:
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
13832 = 2^3 + 24^3 = 18^3 + 20^3
39312 = 2^3 + 34^3 = 15^3 + 33^3
46683 = 3^3 + 36^3 = 27^3 + 30^3
32832 = 4^3 + 32^3 = 18^3 + 30^3
40033 = 9^3 + 34^3 = 16^3 + 33^3
20683 = 10^3 + 27^3 = 19^3 + 24^3
65728 = 12^3 + 40^3 = 31^3 + 33^3
64232 = 17^3 + 39^3 = 26^3 + 36^3
Click me to see the sample solution

35. Write a program in C++ to Check if a number is Mersenne number or not. Go to the editor
Sample Output:
Input a number: 31
31 is a Mersenne number.
Click me to see the sample solution

36. Write a program in C++ to generate Mersenne primes within a range of numbers. Go to the editor
Sample Output:
Input a upper limit [range from 1 to upper limit]: 200
Mersenne prime numbers are:
3 7 31 127
Click me to see the sample solution

37. Write a program in C++ to find Narcissistic decimal numbers within a specific range. Go to the editor
Sample Output:
Input the lower limit: 25
Input a upper limit: 200
The narcissistic decimal numbers between 25 and 200 are:
153
Click me to see the sample solution

38. Write a program in C++ to check whether a given number is palindrome or not. Go to the editor
Sample Output:
Input a number: 141
141 is a Palindrome Number.

39. Write a program in C++ to print the first 20 numbers of the Pell series. Go to the editor
Sample Output:

``` The first 20 numbers of Pell series are:
0 1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832
1136689 2744210 6625109```
Click me to see the sample solution

40. Write a program in C++ to check if a number is Keith or not. Go to the editor
Sample Output:
Input a number : 742
The given number is a Keith Number.
Click me to see the sample solution

41. Write a program in C++ to check if a number is Keith or not(with explanation). Go to the editor
Sample Output:
Input a number : 197
1 + 9 + 7 = 17
9 + 7 + 17 = 33
7 + 17 + 33 = 57
17 + 33 + 57 = 107
33 + 57 + 107 = 197
The given number is a Keith Number.
Click me to see the sample solution

42. Write a program in C++ to create the first twenty Hamming numbers. Go to the editor
Sample Output:

```Input the upper limit of Hamming numbers: 20
The Hamming numbers are:
1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,361,2,3,4,5,6,8,9,10,1
2,15,16,18,20,24,25,27,30,32,361,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,
27,30,32,361,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,361,2,3,4,5
,6,8,9,10,12,15,16,18,20,24,25,27,30,32,361,2,3,4,5,6,8,9,10,12,15,16,1
8,20,24,25,27,30,32,361,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,
361,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,361,2,3,4,5,6,8,9,10
,12,15,16,18,20,24,25,27,30,32,361,2,3,4,5,6,8,9,10,12,15,16,18,20,24,2
5,27,30,32,361,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,361,2,3,4
,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,361,2,3,4,5,6,8,9,10,12,15,16
,18,20,24,25,27,30,32,361,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,3
2,361,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,361,2,3,4,5,6,8,9,
10,12,15,16,18,20,24,25,27,30,32,361,2,3,4,5,6,8,9,10,12,15,16,18,20,24
,25,27,30,32,361,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,361,2,3
,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,361,2,3,4,5,6,8,9,10,12,15,
16,18,20,24,25,27,30,32,361,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30
,32,36
```
Click me to see the sample solution

43. Write a C++ program to check whether a given number is an Armstrong number or not. Go to the editor
Sample Output:

```Input a number: 153
153 is an Armstrong number
```
Click me to see the sample solution

44.Write a program in C++ to find the Armstrong number for a given range of number. Go to the editor
Sample Output:

``` Input starting number of range: 1
Input ending number of range: 1000
Armstrong numbers in given range are:
1 153 370 371 407
```
Click me to see the sample solution

45. Write a program in C++ to check whether a number is a Strong Number or not. Go to the editor
Sample Output:

```Input a number to check whether it is Strong number: 24
24 is not a Strong number
```
Click me to see the sample solution

46. Write a program in C++ to find Strong Numbers within a range of numbers. Go to the editor
Sample Output:

```Input starting range of number: 1
Input ending range of number: 500
The Strong numbers are: 1  2  145
```
Click me to see the sample solution

CPP Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

﻿

## C++ Programming: Tips of the Day

What is a smart pointer and when should I use one?

This answer is rather old, and so describes what was 'good' at the time, which was smart pointers provided by the Boost library. Since C++11, the standard library has provided sufficient smart pointers types, and so you should favour the use of std::unique_ptr, std::shared_ptr and std::weak_ptr.

There was also std::auto_ptr. It was very much like a scoped pointer, except that it also had the "special" dangerous ability to be copied - which also unexpectedly transfers ownership.

It was deprecated in C++11 and removed in C++17, so you shouldn't use it.

```std::auto_ptr<MyObject> p1 (new MyObject());
std::auto_ptr<MyObject> p2 = p1; // Copy and transfer ownership.
// p1 gets set to empty!
p2->DoSomething(); // Works.
p1->DoSomething(); // Oh oh. Hopefully raises some NULL pointer exception.
```

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