The Program in C++ Program to Find Harshad Number between 1 to 1000 is given below:
#include <iostream>
using namespace std;
int sumOfDigits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
bool isHarshadNumber(int n) {
return n % sumOfDigits(n) == 0;
}
int main() {
cout << "Hello Codeauri Family, the Harshad numbers between 1 and 1000 are: \n";
for (int i = 1; i <= 1000; i++) {
if (isHarshadNumber(i)) {
cout << i << " ";
}
}
cout << endl;
return 0;
}
Output:
Hello Codeauri Family, the Harshad numbers between 1 and 1000 are:
1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 45 48 50 54 60 63 70 72 80 81 84 90 100 102 108 110 111 112 114 117 120 126 132 133 135 140 144 150 152 153 156 162 171 180 190 192 195 198 200 201 204 207 209 210 216 220 222 224 225 228 230 234 240 243 247 252 261 264 266 270 280 285 288 300 306 308 312 315 320 322 324 330 333 336 342 351 360 364 370 372 375 378 392 396 399 400 402 405 407 408 410 414 420 423 432 440 441 444 448 450 460 465 468 476 480 481 486 500 504 506 510 511 512 513 516 518 522 531 540 550 552 555 558 576 588 592 594 600 603 605 612 621 624 629 630 640 644 645 648 660 666 684 690 700 702 704 711 715 720 730 732 735 736 738 756 770 774 777 780 782 792 800 801 803 804 810 820 825 828 832 840 846 864 870 874 880 882 888 900 902 910 912 915 918 935 936 954 960 966 972 990 999 1000
Pro-Tips💡
This program uses the same sumOfDigits
and isHarshadNumber
functions as in the previous program.
The main
function loops through all numbers between 1 and 1000 and calls the isHarshadNumber
function for each number.
If the function returns true, the number is a Harshad number and is printed.
The output is a list of all Harshad numbers between 1 and 1000.
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.