Chương trình Python để in các số chia hết cho 5 bằng vòng lặp while

Viết chương trình Python để tìm các số chia hết cho 7 và bội số của 5, trong khoảng từ 1500 đến 2700 [bao gồm cả hai]

Trình bày bằng hình ảnh


Giải pháp mẫu

Mã Python

nl=[]
for x in range[1500, 2701]:
    if [x%7==0] and [x%5==0]:
        nl.append[str[x]]
print [','.join[nl]]

Đầu ra mẫu

1505,1540,1575,1610,1645,1680,1715,1750,1785,1820,1855,1890,1925,1960,1995,2030,2065,2100,2135,2170,2205,2240,
2275,2310,2345,2380,2415,2450,2485,2520,2555,2590,2625,2660,2695 

Sơ đồ


Trực quan hóa việc thực thi mã Python

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực thi chương trình nói trên

Trình chỉnh sửa mã Python

Có một cách khác để giải quyết giải pháp này?

Trước. Bài tập câu lệnh điều kiện và vòng lặp trong Python Trang chủ
Kế tiếp. Viết chương trình Python để chuyển đổi nhiệt độ sang và từ độ C, độ F

Mức độ khó của bài tập này là gì?

Dễ dàng trung bình khó

Kiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource



Theo dõi chúng tôi trên FacebookTwitter để cập nhật thông tin mới nhất.

con trăn. Lời khuyên trong ngày

lớp liệt kê

Đây là một cách tiếp cận sang trọng khác từ Python, không có ý định chơi chữ. Lớp enum của Python sẽ cho phép bạn tạo các thành viên đối tượng enum với các giá trị không đổi và duy nhất mà sau đó có thể được liệt kê, so sánh hoặc sử dụng để nhận dạng

Tiếp cận. Ví dụ: giả sử N = 20 làm giới hạn, thì chương trình sẽ in tất cả các số nhỏ hơn 20 chia hết cho cả 3 và 5. Đối với điều này, hãy chia mỗi số từ 0 đến N cho cả 3 và 5 và kiểm tra phần còn lại của chúng. Nếu phần còn lại là 0 trong cả hai trường hợp thì chỉ cần in số đó

Dưới đây là việc thực hiện.  

C++




// C++ program to print all the numbers

// divisible by 3 and 5 for a given number

#include

using namespace std;

 

// Result function with N

0 15 30 45 
0
0 15 30 45 
1
0 15 30 45 
2
0 15 30 45 
3

0 15 30 45 
4

0 15 30 45 
5_______2_______6

0 15 30 45 
5_______2_______8
0 15 30 45 
9
0 15 30 45 
2
0 15 30 45 
1

0 15 30 45 
5_______2_______4

0 15 30 45 
4_______2_______5

0 15 30 45 
4_______2_______7
0 15 30 45 
8

0 15 30 45 
9// C++ program to print all the numbers0// C++ program to print all the numbers1// C++ program to print all the numbers2

0 15 30 45 
5_______338_______4

// C++ program to print all the numbers4

 

// C++ program to print all the numbers6

0 15 30 45 
2 // C++ program to print all the numbers8

0 15 30 45 
4

0 15 30 45 
5// divisible by 3 and 5 for a given number1

0 15 30 45 
5_______2_______2 // divisible by 3 and 5 for a given number4

0 15 30 45 
5

0 15 30 45 
5_______339_______7

0 15 30 45 
5_______339_______9

0 15 30 45 
5_______340_______1 #include 2

// C++ program to print all the numbers4

 

#include 4

#include 5

Java




#include 6

// divisible by 3 and 5 for a given number

 

#include 8 #include 9

0 15 30 45 
5

0 15 30 45 
5// Result function with N

0 15 30 45 
5_______341_______4
0 15 30 45 
0
0 15 30 45 
1
0 15 30 45 
2
0 15 30 45 
3

0 15 30 45 
5_______2_______4

0 15 30 45 
4_______2_______6

0 15 30 45 
4_______2_______8
0 15 30 45 
9
0 15 30 45 
2 namespace7namespace8namespace9

0 15 30 45 
4_______2_______4

0 15 30 45 
9
0 15 30 45 
5

0 15 30 45 
9
0 15 30 45 
7 std;6std;7 std;8namespace8 // Result function with N0// Result function with N1 std;8namespace8// Result function with N4

// Result function with N5_______344_______6// C++ program to print all the numbers1// Result function with N8

0 15 30 45 
4_______338_______4

0 15 30 45 
5_______338_______4

0 15 30 45 
03

0 15 30 45 
5// C++ program to print all the numbers6

0 15 30 45 
5_______2_______07 using4
0 15 30 45 
0
0 15 30 45 
10

0 15 30 45 
5_______2_______12

0 15 30 45 
4_______339_______1

0 15 30 45 
4_______2_______2
0 15 30 45 
17_______2_______18_______338_______2

0 15 30 45 
20

0 15 30 45 
4_______339_______7

0 15 30 45 
4_______339_______9

0 15 30 45 
5_______338_______4

// C++ program to print all the numbers4

Python3




0 15 30 45 
28

0 15 30 45 
29

 

0 15 30 45 
30

0 15 30 45 
31
0 15 30 45 
32

0 15 30 45 
5

0 15 30 45 
5_______2_______35

0 15 30 45 
5_______2_______8
0 15 30 45 
38
0 15 30 45 
39
0 15 30 45 
40
0 15 30 45 
41

0 15 30 45 
4

0 15 30 45 
9
0 15 30 45 
44

0 15 30 45 
9
0 15 30 45 
7
0 15 30 45 
38_______2_______48 std;7
0 15 30 45 
50
0 15 30 45 
50 namespace8
0 15 30 45 
53
0 15 30 45 
38
0 15 30 45 
48 // Result function with N1
0 15 30 45 
50
0 15 30 45 
50 namespace8
0 15 30 45 
60

// Result function with N5_______2_______62

0 15 30 45 
9
0 15 30 45 
64
0 15 30 45 
65
0 15 30 45 
66 // C++ program to print all the numbers1
0 15 30 45 
68
0 15 30 45 
50
0 15 30 45 
70

// Result function with N5

0 15 30 45 
9
0 15 30 45 
73
0 15 30 45 
60

// Result function with N5____2_______76

 

0 15 30 45 
77

0 15 30 45 
7
0 15 30 45 
79
0 15 30 45 
50
0 15 30 45 
50
0 15 30 45 
82
0 15 30 45 
60

0 15 30 45 
5

0 15 30 45 
5_______2_______86

0 15 30 45 
5_______2_______88_______2_______50
0 15 30 45 
18

0 15 30 45 
5

0 15 30 45 
5_______2_______93

0 15 30 45 
5_______2_______95

C#




0 15 30 45 
96

// divisible by 3 and 5 for a given number

using

0 15 30 45 
99

0 15 30 45 
07 #include 8 #include 9

0 15 30 45 
5

0 15 30 45 
5// Result function with N

0 15 30 45 
5_______341_______4
0 15 30 45 
0
0 15 30 45 
1
0 15 30 45 
2
0 15 30 45 
3

0 15 30 45 
5_______2_______4

0 15 30 45 
4_______2_______6

0 15 30 45 
4_______2_______8
0 15 30 45 
9
0 15 30 45 
2
0 15 30 45 
1

0 15 30 45 
4_______2_______4

0 15 30 45 
9
0 15 30 45 
5

0 15 30 45 
9
0 15 30 45 
7
0 15 30 45 
8

// Result function with N5_______2_______29// C++ program to print all the numbers1// Result function with N8

0 15 30 45 
4_______338_______4

0 15 30 45 
5_______338_______4

0 15 30 45 
5

0 15 30 45 
5// C++ program to print all the numbers6

0 15 30 45 
5_______341_______4
0 15 30 45 
07
0 15 30 45 
0
0 15 30 45 
43

0 15 30 45 
4_______339_______1

0 15 30 45 
4_______2_______2 // divisible by 3 and 5 for a given number4

0 15 30 45 
4_______339_______7

0 15 30 45 
4_______339_______9

0 15 30 45 
5_______338_______4

0 15 30 45 
55

// C++ program to print all the numbers4

PHP




0 15 30 45 
57

0 15 30 45 
58

// divisible by 3 and 5 for a given number

 

// Result function with N

0 15 30 45 
61
0 15 30 45 
1
0 15 30 45 
63// Result function with N4

0 15 30 45 
12

0 15 30 45 
5_______2_______6

0 15 30 45 
5_______2_______8
0 15 30 45 
9
0 15 30 45 
71
0 15 30 45 
72
0 15 30 45 
71
0 15 30 45 
74
0 15 30 45 
63// C++ program to print all the numbers2
0 15 30 45 
71
0 15 30 45 
78

0 15 30 45 
5_______2_______12

0 15 30 45 
4_______2_______5

0 15 30 45 
4_______2_______7
0 15 30 45 
9
0 15 30 45 
71
0 15 30 45 
87
0 15 30 45 
71
0 15 30 45 
89

0 15 30 45 
9
0 15 30 45 
91
0 15 30 45 
71
0 15 30 45 
93// C++ program to print all the numbers1// C++ program to print all the numbers2

0 15 30 45 
5_______338_______4

// C++ program to print all the numbers4

 

// C++ program to print all the numbers6

0 15 30 45 
5

// divisible by 3 and 5 for a given number1

0 15 30 45 
63 // C++ program to print all the numbers03

 

// divisible by 3 and 5 for a given number7

0 15 30 45 
1
0 15 30 45 
63// Result function with N8

 

// C++ program to print all the numbers08

// C++ program to print all the numbers09

Javascript




// C++ program to print all the numbers10

 

// C++ program to print all the numbers11

// C++ program to print all the numbers12

// C++ program to print all the numbers13

// C++ program to print all the numbers14

 

// Result function with N

0 15 30 45 
61
0 15 30 45 
95

0 15 30 45 
12

0 15 30 45 
5_______2_______6

0 15 30 45 
5_______2_______8 // C++ program to print all the numbers23

0 15 30 45 
5_______2_______12

0 15 30 45 
4_______2_______5

0 15 30 45 
4_______2_______7
0 15 30 45 
8

0 15 30 45 
9// C++ program to print all the numbers32// C++ program to print all the numbers1// Result function with N8

0 15 30 45 
5_______338_______4

// C++ program to print all the numbers4

 

// C++ program to print all the numbers6

0 15 30 45 
5

// divisible by 3 and 5 for a given number1

// C++ program to print all the numbers41

 

// divisible by 3 and 5 for a given number7

// divisible by 3 and 5 for a given number9

 

// C++ program to print all the numbers44

 

// C++ program to print all the numbers45

Đầu ra

0 15 30 45 60 75 90 

Thời gian phức tạp. TRÊN]
Không gian phụ trợ. Ô[1]

Phương pháp. Điều này cũng có thể được thực hiện bằng cách kiểm tra xem số đó có chia hết cho 15 hay không, vì BCNN của 3 và 5 là 15 và bất kỳ số nào chia hết cho 15 cũng chia hết cho 3 và 5 và ngược lại.  

C++




// C++ program to print all the numbers46

// C++ program to print all the numbers47

#include

using namespace std;

 

0 15 30 45 
2 // C++ program to print all the numbers8

0 15 30 45 
12

// C++ program to print all the numbers55______2_______2 // C++ program to print all the numbers57

// C++ program to print all the numbers55_______2_______8

0 15 30 45 
9
0 15 30 45 
2 // C++ program to print all the numbers62

// C++ program to print all the numbers55______2_______12

 

0 15 30 45 
5_______338_______66

0 15 30 45 
5_______2_______7// C++ program to print all the numbers69

// C++ program to print all the numbers70______338_______71______338_______1// C++ program to print all the numbers2

0 15 30 45 
5_______338_______4

// C++ program to print all the numbers55_______338_______4

// C++ program to print all the numbers55_______340_______1 #include 2

// C++ program to print all the numbers4

 

// C++ program to print all the numbers82

Java




// C++ program to print all the numbers83

 

// C++ program to print all the numbers84 // C++ program to print all the numbers85

 

#include 8 // C++ program to print all the numbers87

0 15 30 45 
5_______2_______07 using4
0 15 30 45 
0 // C++ program to print all the numbers92

// C++ program to print all the numbers70

// C++ program to print all the numbers94_______2_______2 // C++ program to print all the numbers96// C++ program to print all the numbers97// C++ program to print all the numbers2

0 15 30 45 
4_______2_______8
0 15 30 45 
9
0 15 30 45 
2 // divisible by 3 and 5 for a given number03namespace8// divisible by 3 and 5 for a given number05// C++ program to print all the numbers97// divisible by 3 and 5 for a given number07

 

0 15 30 45 
9// divisible by 3 and 5 for a given number09

0 15 30 45 
9
0 15 30 45 
7 // divisible by 3 and 5 for a given number12_______339_______13 std;8namespace8// divisible by 3 and 5 for a given number16

// Result function with N5_______339_______18// C++ program to print all the numbers1// Result function with N8

0 15 30 45 
9// C++ program to print all the numbers4

0 15 30 45 
4_______338_______4

// C++ program to print all the numbers70

0 15 30 45 
5_______338_______4

// C++ program to print all the numbers4

 

// C++ program to print all the numbers82

Python3




// divisible by 3 and 5 for a given number30

// divisible by 3 and 5 for a given number31

// divisible by 3 and 5 for a given number32_______2_______50// C++ program to print all the numbers97

0 15 30 45 
8 // divisible by 3 and 5 for a given number36
0 15 30 45 
39
0 15 30 45 
40
0 15 30 45 
9namespace8// divisible by 3 and 5 for a given number41

// C++ program to print all the numbers55_______339_______43

// C++ program to print all the numbers55_______2_______7 // divisible by 3 and 5 for a given number36

0 15 30 45 
48// divisible by 3 and 5 for a given number13
0 15 30 45 
50
0 15 30 45 
50namespace8
0 15 30 45 
60

0 15 30 45 
5_______2_______62// divisible by 3 and 5 for a given number55
0 15 30 45 
50// C++ program to print all the numbers1// Result function with N4

C#




using

0 15 30 45 
99

 

0 15 30 45 
07 #include 8 // C++ program to print all the numbers87

 

0 15 30 45 
5_______341_______4
0 15 30 45 
07
0 15 30 45 
0 // divisible by 3 and 5 for a given number68

0 15 30 45 
5_______2_______12

 

0 15 30 45 
4_______2_______2 // C++ program to print all the numbers57

0 15 30 45 
4_______2_______8
0 15 30 45 
9
0 15 30 45 
2 // divisible by 3 and 5 for a given number78

 

0 15 30 45 
9// divisible by 3 and 5 for a given number09

0 15 30 45 
9
0 15 30 45 
7 // divisible by 3 and 5 for a given number83

// Result function with N5_______339_______85// C++ program to print all the numbers1// Result function with N8

0 15 30 45 
9// C++ program to print all the numbers4

0 15 30 45 
4_______338_______4

0 15 30 45 
5_______338_______4

// C++ program to print all the numbers4

// C++ program to print all the numbers55

// C++ program to print all the numbers55_______338_______82

Javascript




// C++ program to print all the numbers10

// divisible by 3 and 5 for a given number99

// C++ program to print all the numbers55_______2_______8#include 02

// C++ program to print all the numbers55______2_______12

 

0 15 30 45 
5_______338_______66

0 15 30 45 
5_______2_______7// C++ program to print all the numbers69

// C++ program to print all the numbers70_______340_______11____338_______1// Result function with N8

0 15 30 45 
5_______338_______4

// C++ program to print all the numbers55_______338_______4

// C++ program to print all the numbers45

Đầu ra

0 15 30 45 

Thời gian phức tạp. Trên]
Không gian phụ trợ. Ô[1]

Phương pháp 3. chúng tôi nhận thấy rằng LCM của 3 & 5 là 15, vì vậy chúng tôi không cần lặp lại toàn bộ vòng lặp từ 0 đến n nhưng chúng tôi cần lặp lại từ 0 và mỗi lần tăng i lên 15 để theo cách này chúng tôi có thể giảm độ phức tạp của thời gian khi chúng tôi

C++




#include 19

#include

using namespace std;

 

0 15 30 45 
2 // C++ program to print all the numbers8

0 15 30 45 
12

0 15 30 45 
5_______2_______2 // C++ program to print all the numbers57

0 15 30 45 
5_______339_______09

0 15 30 45 
5_______340_______33

 

0 15 30 45 
5_______340_______35

0 15 30 45 
5_______2_______8
0 15 30 45 
9
0 15 30 45 
2 #include 40

0 15 30 45 
4_______338_______71____338_______1// C++ program to print all the numbers2

0 15 30 45 
5_______338_______4

0 15 30 45 
5_______340_______1 #include 2

// C++ program to print all the numbers4

Java




#include 51

#include 8 // C++ program to print all the numbers87

// C++ program to print all the numbers55_______2_______07 using4

0 15 30 45 
0 #include 58

0 15 30 45 
5_______2_______2 // C++ program to print all the numbers96// C++ program to print all the numbers97// C++ program to print all the numbers2

 

0 15 30 45 
5_______339_______09

0 15 30 45 
5_______340_______33

 

0 15 30 45 
5_______340_______35

0 15 30 45 
5_______2_______8
0 15 30 45 
9
0 15 30 45 
2 // divisible by 3 and 5 for a given number03namespace8#include 76// divisible by 3 and 5 for a given number13// divisible by 3 and 5 for a given number16

// C++ program to print all the numbers70______340_______80// C++ program to print all the numbers1// Result function with N8

0 15 30 45 
5_______338_______4

// C++ program to print all the numbers55_______338_______4

// C++ program to print all the numbers4

 

#include 88

C#




#include 89

using

0 15 30 45 
99

 

0 15 30 45 
07 #include 8 // C++ program to print all the numbers87

0 15 30 45 
5_______2_______07 using4
0 15 30 45 
0 #include 99using00using01

0 15 30 45 
4_______2_______2 // C++ program to print all the numbers57

 

0 15 30 45 
4_______339_______09

0 15 30 45 
4_______340_______33

 

0 15 30 45 
4_______340_______35

0 15 30 45 
4_______2_______8
0 15 30 45 
9
0 15 30 45 
2 #include 40

0 15 30 45 
9using17_______338_______1// Result function with N8

0 15 30 45 
4_______338_______4

0 15 30 45 
5_______338_______4

// C++ program to print all the numbers4

 

using25

Đầu ra

0 15 30 45 

Thời gian phức tạp. O[n/15] ~= O[n] [tốt hơn nhiều so với cả hai phương pháp trên vì chúng ta chỉ cần lặp lại i trong n/15 lần]

những số nào chia hết cho 5?

Quy tắc chia hết của 5 . Ví dụ: 35, 790 và 55 đều chia hết cho 5. If a number ends with 0 or 5, it is divisible by 5. For example, 35, 790, and 55 are all divisible by 5.

Điều nào sau đây sẽ xác định xem một số có chia hết cho 5 trong lập trình hay không?

Một số chia hết cho 5 nếu các chữ số tận cùng của nó là 0 hoặc 5 . Hình minh họa. Ví dụ: chúng ta hãy xem xét 769555 Số được tạo bởi chữ số cuối cùng là = 5 Vì 5 chia hết cho 5 nên câu trả lời là CÓ.

Số nguyên có chia hết cho 5 không?

Chia hết cho 5 . Thí dụ. 9375 chia hết cho 5 vì chữ số tận cùng là 5. 8417 không chia hết cho 5, vì chữ số cuối cùng không phải là 0 hoặc 5. An integer is divisible by 5 if the last digit is either 0 or 5 . Example: 9375 is divisible by 5 , since the last digit is a 5 . 8417 is not divisible by 5 , since the last digit is not a 0 or a 5 .

Số nào sau đây chia hết cho 5*?

Một số chia hết cho 5 nếu chữ số tận cùng của số đó là 0 hoặc 5. Chia hết cho 5 - ví dụ. Các số 105, 275, 315, 420, 945, 760 chia hết cho 5.

Chủ Đề