Số có một chữ số trong chương trình Python

Trong python, String cung cấp toán tử [ ] để truy cập bất kỳ ký tự nào trong chuỗi theo vị trí chỉ mục. Chúng ta cần chuyển vị trí chỉ mục trong dấu ngoặc vuông và nó sẽ trả về ký tự tại chỉ mục đó. Vì việc lập chỉ mục các ký tự trong một chuỗi bắt đầu từ 0, nên để lấy ký tự đầu tiên của chuỗi đã cho, hãy chuyển vị trí chỉ mục 0 trong toán tử [ ]

# Python Program to get the first digit of number

# take input
num = int[input['Enter any Number: ']]

# convert int to string
num_str = str[num]

# get the first digit
first_digit = num_str[0]

# printing first digit of number
print['The first digit of number:', first_digit]

đầu ra. -

Nhập số bất kỳ. 7983516
Chữ số đầu tiên của số. 7

Nhận chữ số đầu tiên của số trong Python bằng cách sử dụng Slicing

Chúng ta sẽ lấy ký tự đầu tiên của chuỗi bằng toán tử lát. Các [. 1] chỉ định ký tự tại chỉ mục 0. Chuỗi [. 1] chỉ định các ký tự đầu tiên của chuỗi đã cho

# Python Program to get the first digit of number

# take input
num = int[input['Enter any Number: ']]

# convert int to string
num_str = str[num]

# get the first digit
first_digit = num_str[:1]

# printing first digit of number
print['The first digit of number:', first_digit]

đầu ra. -

Nhập số bất kỳ. 100
Chữ số đầu tiên của số. 1

Q] Viết chương trình in chữ số tại vị trí của một số đã cho bằng Python

num = 543169

while [num >= 10]:
   num = num // 10

print[num]

đầu ra. - 5

Nhận ghi chú để làm cho quá trình học tập của bạn dễ dàng. Chúng được thiết kế đặc biệt cho những người mới bắt đầu muốn học viết mã thông qua các từ, chương trình và ví dụ đơn giản. Bạn có thể sử dụng nó làm tài liệu tham khảo và cho mục đích sửa đổi

Giả sử ta có số dương n, ta sẽ cộng tất cả các chữ số của nó để được số mới. Bây giờ lặp lại thao tác này cho đến khi nhỏ hơn 10

Vì vậy, nếu đầu vào là 9625, thì đầu ra sẽ là 4

Để giải quyết vấn đề này, chúng tôi sẽ làm theo các bước sau -

  • Xác định một phương thức giải quyết [], điều này sẽ mất n
  • nếu n < 10 thì
  • S. = 0
  • tôi. = sàn của [log[n] cơ số 10 + 1]
  • trong khi l > 0, làm
    • S. = s + [n mod 10]
    • N. = thương của n/10
    • tôi. = l - 1
  • trở lại giải quyết [s]

Chúng ta hãy xem triển khai sau đây để hiểu rõ hơn -

Thí dụ

Bản thử trực tiếp

import math
class Solution:
   def solve[self, n]:
      if n < 10:
         return n
      s = 0
      l = math.floor[math.log[n, 10] + 1]
      while l > 0:
         s += n % 10
         n //= 10
         l -= 1
      return self.solve[s]
ob = Solution[]
print[ob.solve[9625]]

Đầu vào

9625

đầu ra

4

Bạn được cho một số nguyên dương N, hãy viết chương trình cộng liên tục các chữ số của số đó cho đến khi số đó trở thành số có một chữ số

Đầu vào

Đầu vào phải là một dòng chứa số có một chữ số

đầu ra

Đầu ra phải là một dòng chứa một số có một chữ số

Giải trình

Trong ví dụ, số đã cho là 92. Vì số này có nhiều hơn một chữ số, hãy liên tục thêm các chữ số như

Để kiểm tra xem một số có chia hết cho 9 hay không, hãy cộng các chữ số của số đó và kiểm tra xem tổng đó có chia hết cho 9 hay không. Nếu có, là trường hợp, số đó chia hết cho 9, nếu không thì không

hãy lấy 27  tôi. e [2+7 = 9] do đó chia hết cho 9
Nếu một số n chia hết cho 9 thì tổng các chữ số của nó cho đến khi tổng đó có một chữ số luôn là 9. Ví dụ,
Giả sử, n = 2880
Tổng các chữ số = 2 + 8 + 8 = 18. 18 = 1 + 8 = 9

Vì vậy,
Một số có thể ở dạng 9x hoặc 9x + k. Đối với trường hợp đầu tiên, câu trả lời luôn là 9. Đối với trường hợp thứ hai, và luôn là k là số dư còn lại

Vấn đề được biết đến rộng rãi là vấn đề gốc chữ số

Bạn có thể thấy bài viết Wikipedia này hữu ích. -> https. // vi. wikipedia. org/wiki/Digital_root

Dưới đây là triển khai ý tưởng trên.  

C++




#include

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
0
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
1
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
2

 

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
3
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
4
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
3
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
6

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
7

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
8
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
4
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
5

9
76_______50_______1 // digits of a number until5

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
8// C++ program to find sum of1
9
81

9
3

 

// C++ program to find sum of4

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
3 // C++ program to find sum of6

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
7

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
8
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
3
9
89

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
8
9
91

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
8// C++ program to find sum of1 // digits of a number until5

9
3

Java




// sum becomes single digit.0

9
97

 

// sum becomes single digit.3

1
23

 

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
8// sum becomes single digit.7
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
3
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
4
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
3
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
6

Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
8
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
7

If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
3
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
4
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
06#include7_______6_______01

If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
3// C++ program to find sum of1 #include7#include8

If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
3// C++ program to find sum of1 // C++ program to find sum of19
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
00 // C++ program to find sum of21#include7// C++ program to find sum of23
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
00 // C++ program to find sum of25
Input : 1234
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum[x] == 10
              Hence ans will be 1+0 = 1

Input : 5674
Output : 4 
00
If n == 0
   return 0;

If n % 9 == 0      
    digSum[n] = 9
Else               
    digSum[n] = n % 9 
56

Chủ Đề