数の因数を見つけるPythonプログラム

Python

ソースコード

# Python Program to find the factors of a number

# This function computes the factor of the argument passed
def print_factors(x):
   print("The factors of",x,"are:")
   for i in range(1, x + 1):
       if x % i == 0:
           print(i)

num = 320

print_factors(num)

出力

The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320

注意: 別の数の因数を見つけるには、の値を変更します num

このプログラムでは、因子が見つかる番号がに格納されます num、に渡されます print_factors() 関数。 この値は変数に割り当てられます バツprint_factors()

関数では、 for から繰り返すループ に等しい バツ。 場合 バツ で完全に割り切れる 、それはの要因です バツ



Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です