Program Python untuk Menemukan Faktor-faktor Angka

5
(1)

Kode sumber

# 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)

Keluaran

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

catatan: Untuk mencari faktor dari bilangan lain, ubah nilai num.

Dalam program ini, nomor yang faktornya ditemukan disimpan num, yang diteruskan ke print_factors() fungsi. Nilai ini diberikan ke variabel x di print_factors().

Dalam fungsinya, kami menggunakan for loop untuk mengulang dari saya sama dengan x. Jika x habis habis dibagi saya, itu adalah faktor x.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.