Hướng dẫn python program to find the sum of digits in a number without recursion - chương trình python để tìm tổng các chữ số trong một số mà không cần đệ quy
Khi bắt buộc phải tìm tổng các chữ số trong một số mà không sử dụng phương thức đệ quy, toán tử ‘%, toán tử‘++và toán tử ‘//, có thể được sử dụng. Show Dưới đây là một cuộc biểu tình cho cùng một - Thí dụ& nbsp; bản demo trực tiếp def sum_of_digits(my_num): sum_val = 0 while (my_num != 0): sum_val = sum_val + (my_num % 10) my_num = my_num//10 return sum_val my_num = 12345671 print("The number is : ") print(my_num) print("The method to calculate sum of digits is being called...") print("The sum of " +str(my_num) + " is : ") print(sum_of_digits(my_num)) Đầu raThe number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 29 Giải trình
Cập nhật vào ngày 12 tháng 12 năm 2021 12:04:56
Cải thiện bài viết Lưu bài viết Cho một số và nhiệm vụ là tìm tổng số các chữ số của số này trong Python. & Nbsp; ví dụ: & nbsp; & nbsp;
& nbsp; bên dưới là các phương thức để tổng của các chữ số. & nbsp; Phương thức-1: sử dụng các phương thức str () và int () .: Phương thức str () được sử dụng để chuyển đổi số thành chuỗi. Phương thức int () được sử dụng để chuyển đổi chữ số chuỗi thành số nguyên. & Nbsp; Chuyển đổi số thành chuỗi và lặp qua từng chữ số trong chuỗi và sau khi chuyển đổi từng chữ số thành số nguyên và thêm vào tổng của các chữ số trong mỗi lần lặp. & Nbsp; Python3
The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 290 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 291
The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 293 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 294 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 295 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 296 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 297 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 298 sum 150 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 290 152 153
155 sum 157 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 290 159 150 151 Output: 15 Phương thức-2: Sử dụng phương thức SUM () .: Phương thức SUM () được sử dụng để tổng số trong danh sách.The sum() method is used to sum of numbers in the list. Chuyển đổi số thành chuỗi bằng str () và dải chuỗi và chuyển đổi thành danh sách số bằng phương thức dải () và map () Phương thức resp. Sau đó tìm tổng bằng phương thức Sum (). Python3
The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 290 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 291
The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 293 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 294 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 295 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 296 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 297 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 298 sum 150 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 290 152 153 157 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 290 159 150 151 Output: 15
Nhận số Python3
The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 290 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 291
The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 293 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 294 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 295 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 296 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 297 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 298 sum 150 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 290 152 153
155 sum
155 sum 157 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 290 159 150 151 Output: 15 Phương thức-2: Sử dụng phương thức SUM () .: Phương thức SUM () được sử dụng để tổng số trong danh sách. Python3Chuyển đổi số thành chuỗi bằng str () và dải chuỗi và chuyển đổi thành danh sách số bằng phương thức dải () và map () Phương thức resp. Sau đó tìm tổng bằng phương thức Sum ().
155 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 290 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 296 158 157 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 290 159 150 The number is : 12345671 The method to calculate sum of digits is being called... The sum of 12345671 is : 2920 Output: 15 |