armstrong公理系统_Python程序使用面向对象的方法检查Armstrong号码

armstrong公理系统

Armstrong Number - An Armstrong Number is a Number which is equal to it's sum of digit's cube. For example - 153 is an Armstrong number: here 153 = (1*1*1) + (5*5*5) + (3*3*3).

阿姆斯壮数字 -阿姆斯壮数字是一个数字,等于它的数字和。 例如-153是一个阿姆斯特朗数:此处153 =(1 * 1 * 1)+(5 * 5 * 5)+(3 * 3 * 3)

This program will take a number and check whether it is Armstrong Number or Not.

该程序将使用一个数字,并检查它是否是Armstrong号码。

Steps for checking Armstrong number:

检查阿姆斯壮编号的步骤:

  1. Calculate sum of each digit's cube of a number.

    计算一个数字的每个数字的立方的总和。

  2. Compare that number with the resultant sum.

    将该数字与结果总和进行比较。

  3. If Number and Sum of digit's cube is equal then it is an Armstrong Number otherwise not.

    如果数字的立方和数字的总和相等,则它是一个阿姆斯壮数字,否则就不是。

We are implementing this program using the concept of classes and objects.

我们正在使用类和对象的概念来实现该程序。

Firstly we create the Class with "Check" name with 1 attributes (number) and 2 methods, the methods are:

首先,我们使用“ Check”名称创建具有1个属性( number )和2个方法的类,这些方法是:

  1. Constructor Method: This is created using __init__ inbuilt keyword. The constructor method is used to initialize the attributes of the class at the time of object creation.

    构造方法 :这是使用__init__内置关键字创建的。 构造函数方法用于在创建对象时初始化类的属性。

  2. Object Method: isArmstrong() is the object method, for creating object method we have to pass at least one parameter i.e. self keyword at the time of function creation. This Object method has no use in this program.

    对象方法 : isArmstrong()是对象方法,要创建对象方法,我们必须在函数创建时传递至少一个参数,即self关键字。 该Object方法在此程序中没有用。

Secondly, we have to create an object of this class using a class name with parenthesis then we have to call its method for our output.

其次,我们必须使用带有括号的类名来创建此类的对象,然后必须为其输出调用其方法。

Below is the implementation of the program,

下面是该程序的实现,

Python代码来计数创建的对象数 (Python code to count number of objects created)

# Define a class for Checking Armstrong number
class Check :

    # Constructor
    def __init__(self,number) :
        self.num = number
        
    # define a method for checking number is Armstrong or not 
    def isArmstrong(self) :

        # copy num attribute to the temp variable
        temp = self.num
        res = 0

        # run the loop untill temp is not equal to zero
        while(temp != 0) :
            
            rem = temp % 10

            res += rem ** 3

            # integer division
            temp //= 10

        # check result equal to the num attribute or not
        if self.num == res :
            print(self.num,"is Armstrong")
        else :
            print(self.num,"is not Armstrong")


# Driver code 
if __name__ == "__main__" :
    
    # input number
    num = 153
    
    # make an object of Check class
    check_Armstrong = Check(num)
    
    # check_Armstrong object's method call
    check_Armstrong.isArmstrong()
    
    num = 127
    check_Armstrong = Check(num)
    check_Armstrong.isArmstrong()      

Output

输出量

153 is Armstrong
127 is not Armstrong


翻译自: https://www.includehelp.com/python/program-to-check-armstrong-number-using-object-oriented-approach.aspx

armstrong公理系统

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值