×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Python Armstrong Number print

Python Armstrong Number print

Python Armstrong Number print

(OP)
I need to print all armstrong numbers ranging from 1 to 10000. My issue is that when my programme runs and hits 150, it does nothing.

CODE --> python

(1^3) + ((6^3)-2) + (0^3) 

as opposed to

CODE --> python

(1^3) + (6^3) + (0^3) 

As a result, it does not display 153 (an Armstrong number), because the total is 152. I'm not sure whether other numbers are performing the same thing. Yet, as shown here, I checked up to 200 and there is no problem with any other numbers except those in the 150-160 area.

Is this a compiler bug? Is it necessary to reinstall my compiler? I'm currently utilising codeblocks.

CODE --> python

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    for(int i = 0;i <= 1000;++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 << i << ' ';
    }
    cout << "\n\n\n";
    return 0;
} 


RE: Python Armstrong Number print

Hi soni21,

The power operator in python is not ^ but **:
>>> 1**3 + 5**3 + 3**3
153

The code on the link you provided works well
https://www.scaler.com/topics/armstrong-number-in-...

I tried it

CODE

# https://www.scaler.com/topics/armstrong-number-in-python/

def check_3_digit_armstrong(number):
  temp = number
  add_sum = 0
  while temp != 0:
    k = temp % 10
    add_sum += k*k*k
    temp = temp//10
  if add_sum == number:
    print('The number %3d is a three-digit Armstrong Number' % number)

def check_n_digit_armstrong(number):
  digits = len(str(number))
  temp = number
  add_sum = 0
  while temp != 0:
    # get the last digit in the number
    k = temp % 10
    # find k^digits
    add_sum += k**digits
    # floor division
    # which update number with second last digit as last digit
    temp = temp//10
  if add_sum == number:
    print('The number %6d is %d-digit Armstrong Number' % (number, digits))
    
if __name__ == "__main__":
  print("3-digit Armstrong numbers")
  for number in range(100, 1000):
    check_3_digit_armstrong(number)

  print()

  print("n-digit Armstrong numbers")
  for number in range(100, 1000001):
    check_n_digit_armstrong(number) 

and got this output:

CODE

3-digit Armstrong numbers
The number 153 is a three-digit Armstrong Number
The number 370 is a three-digit Armstrong Number
The number 371 is a three-digit Armstrong Number
The number 407 is a three-digit Armstrong Number

n-digit Armstrong numbers
The number    153 is 3-digit Armstrong Number
The number    370 is 3-digit Armstrong Number
The number    371 is 3-digit Armstrong Number
The number    407 is 3-digit Armstrong Number
The number   1634 is 4-digit Armstrong Number
The number   8208 is 4-digit Armstrong Number
The number   9474 is 4-digit Armstrong Number
The number  54748 is 5-digit Armstrong Number
The number  92727 is 5-digit Armstrong Number
The number  93084 is 5-digit Armstrong Number
The number 548834 is 6-digit Armstrong Number 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close