Hướng dẫn how do you print the factors of a number in python for loop? - làm thế nào để bạn in các yếu tố của một số trong python cho vòng lặp?

Mã nguồn

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

Đầu ra

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

Lưu ý: Để tìm các yếu tố của một số khác, hãy thay đổi giá trị của num. To find the factors of another number, change the value of num.

Trong chương trình này, số có hệ số sẽ được tìm thấy được lưu trữ trong num, được chuyển đến hàm

The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320
1. Giá trị này được gán cho biến X trong
The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320
1.

Trong hàm, chúng tôi sử dụng vòng

The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320
3 để lặp lại từ i bằng x. Nếu x hoàn toàn chia hết bởi tôi, thì đó là yếu tố của x.

Trong bài viết này, bạn sẽ học cách tìm các yếu tố của một số sử dụng cho vòng lặp và trong khi vòng lặp trong ngôn ngữ lập trình Python.

Ví dụ

Đầu vào: 60

Các yếu tố của & nbsp; 60 là: 1 2 3 4 5 6 10 12 15 20 30 60 60

Đầu vào: 70

Các yếu tố của & nbsp; 70 là: 1 2 5 7 10 14 35 70

Các yếu tố của một số là gì?

Các yếu tố của một số được định nghĩa là các số chia số ban đầu mà không để lại bất kỳ phần còn lại [nhắc lại trái = 0].[left reminder = 0].

Bạn nên có kiến ​​thức về các chủ đề sau trong chương trình Python để hiểu các chương trình này:

  • Hàm Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    4
  • Hàm Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    5
  • Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    6 Câu lệnh LOOP
  • Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    7 Câu lệnh LOOP
  • Tuyên bố Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    8
  • Hàm Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    9

1. & NBSP; Chương trình Python để tìm các yếu tố của một số sử dụng cho vòng lặp

# Python program to find factors of a number using for loop

print ["Enter the positive integer number: ", end=""]
random_number = int [input []]

print ["The factors of the ", random_number, "are: ", end=""]
for i in range [1, random_number + 1]:
    if random_number % i == 0:
        print [i, end=" "]
print [end="\n"]

Chạy chương trình

Đầu ra

# Python program to find factors of a number using for loop

print ["Enter the positive integer number: ", end=""]
random_number = int [input []]

print ["The factors of the ", random_number, "are: ", end=""]
for i in range [1, random_number + 1]:
    if random_number % i == 0:
        print [i, end=" "]
print [end="\n"]
0

2. & NBSP; Chương trình Python để tìm các yếu tố của một số sử dụng trong khi vòng lặp

# Python program to find factors of a number using while loop

print ["Enter the positive integer number: ", end=""]
random_number, i = int [input []], 1

print ["The factors of the ", random_number, "are: ", end=""]
while i  0:

Trong lời nói, bạn muốn "bắt đầu

# Python program to find factors of a number using for loop

print ["Enter the positive integer number: ", end=""]
random_number = int [input []]

print ["The factors of the ", random_number, "are: ", end=""]
for i in range [1, random_number + 1]:
    if random_number % i == 0:
        print [i, end=" "]
print [end="\n"]
5 TẮT tại
# Python program to find factors of a number using for loop

print ["Enter the positive integer number: ", end=""]
random_number = int [input []]

print ["The factors of the ", random_number, "are: ", end=""]
for i in range [1, random_number + 1]:
    if random_number % i == 0:
        print [i, end=" "]
print [end="\n"]
7 và giảm nó trong khi nó vẫn còn lớn hơn 0, kiểm tra các yếu tố ở mỗi bước".

>>> def factors[n]:
...     i = n
...     while i > 0:  # >> factors[20]
20
10
5
4
2
1

Đã trả lời ngày 7 tháng 7 năm 2014 lúc 22:08Jul 7, 2014 at 22:08

Arshajiiarshajiiarshajii

125K24 Huy hiệu vàng234 Huy hiệu bạc282 Huy hiệu Đồng24 gold badges234 silver badges282 bronze badges

6

Điều kiện trong khi phải là

# Python program to find factors of a number using for loop

print ["Enter the positive integer number: ", end=""]
random_number = int [input []]

print ["The factors of the ", random_number, "are: ", end=""]
for i in range [1, random_number + 1]:
    if random_number % i == 0:
        print [i, end=" "]
print [end="\n"]
8 chứ không phải
# Python program to find factors of a number using for loop

print ["Enter the positive integer number: ", end=""]
random_number = int [input []]

print ["The factors of the ", random_number, "are: ", end=""]
for i in range [1, random_number + 1]:
    if random_number % i == 0:
        print [i, end=" "]
print [end="\n"]
9 vì nó sẽ không bao giờ thỏa mãn nó khi tôi bắt đầu trong 20 [hoặc nhiều trường hợp khác]

Đã trả lời ngày 7 tháng 7 năm 2014 lúc 22:07Jul 7, 2014 at 22:07

0

Hy vọng câu trả lời của tôi sẽ giúp!

#The "while True" program allows Python to reject any string or characters
while True:
try:
    num = int[input["Enter a number and I'll test it for a prime value: "]]
except ValueError:
    print["Sorry, I didn't get that."]
    continue
else:
    break

#The factor of any number contains 1 so 1 is in the list by default.
fact = [1]

#since y is 0 and the next possible factor is 2, x will start from 2.
#num % x allows Python to see if the number is divisible by x
for y in range[num]:
    x = y + 2
    if num % x is 0:
        fact.append[x]
#Lastly you can choose to print the list
print["The factors of %s are %s" % [num, fact]]

Đã trả lời ngày 10 tháng 6 năm 2019 lúc 6:29Jun 10, 2019 at 6:29

Bài Viết Liên Quan

Chủ Đề