c++ - Armstrong numbers. print armstrong numbers -


i kindly request think question have been asked earlier, read on first.

i need print armstrong numbers between 1 , 10000. problem whenever program run , reaches 150, does

(1^3) + ((5^3)-1) + (0^3) 

instead of

(1^3) + (5^3) + (0^3). 

thus not print 153 (which armstrong number), of course because sum results in 152. not know if other numbers doing this. have checked untill 200 , there no problem other numbers except in 150–160 range.

is compiler error. should re-install compiler? using codeblocks.

#include <iostream> #include <math.h>  using namespace std;  int main() {     for(int = 0;i <= 10000;++i)     {         int r = i;         int dig = 0;         while(r != 0)         {             dig++;             r /= 10;         }         int n = i, sum = 0;         while(n != 0)         {             int d = n % 10;             sum += pow(d, dig);             n /= 10;         }         if(sum == i)             cout << << ' ';     }     cout << "\n\n\n";     return 0; } 

you should run code in debugger. code not compile me (gcc 6) because use cout without std:: or using namespace std;. how compile on system? using math.h, in c++ should rather use cmath.

after fixing this, following output on fedora 24 g++ in version 6.4.1:

0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 

the 153 included in there, either compiler has error or program has undefined behavior , therefore error ensues.

i have looked @ definition armstrong numbers , did short python implementation:

# copyright © 2017 martin ueding <dev@martin-ueding.de> # licensed under mit/expat license.  def is_armstrong(number):     digits = [int(letter) letter in str(number)]     score = sum(digit**len(digits) digit in digits)     return score == number  armstrong = list(filter(is_armstrong, range(10000))) print(' '.join(map(str, armstrong))) 

the output matches c++ program on machine exactly:

0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 

looking through code cannot spot undefined behavior, looks sensible. first count number of digits, build sum. perhaps should try other compilers gcc, llvm, or ideone. code blocks ship own compiler or use system compiler? operating system running?


you said learning program. that's cool hear! hope have good c++ book or other resource. c++, there lot of bad advice on internet. make sure have book has @ least c++11, else badly outdated.

i have changed program , created short functions 1 task such easier read , reason about. not sure whether know functions, don't worry if seems complicated :-).

#include <cmath> #include <iostream>  int get_digit_count(int const number) {     int digits = 0;     int remainder = number;      while (remainder > 0) {         ++digits;         remainder /= 10;     }      return digits; }  bool is_armstrong_number(int const number) {     int const digit_count = get_digit_count(number);     int remainder = number;     int sum = 0;      while (remainder > 0) {         int const last_digit = remainder % 10;         sum += std::pow(last_digit, digit_count);         remainder /= 10;     }      return number == sum; }  int main() {     (int = 0; <= 10000; ++i) {         if (is_armstrong_number(i)) {             std::cout << << ' ';         }     }     std::cout << std::endl; } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -