Yêu cầu:
Nhập số tự nhiên n rồi tính tổng (lưu ý phép chia các số nguyên):
S = 1 + 1/2 + 1/3 + 1/4 +… 1/n ( n > 0)
Giải thuật:
– Nhập vào số nguyên dương n (n > 0)
– Dùng vòng lặp for( ) để tính tổng S
test.py
""" # Author: VNCODING # Function: S = 1 + 1/2 + 1/3 + 1/4 + ... + 1/n # 2022/08/17 first create VNCODING """ print("Input N: ") N = input() while int(N) < 1: print("Input N again: ") N = input() S = 0.0 for i in range(1, int(N)+1): S += 1/i print("S = ", S)
Kết quả:
Input N:
-3
Input N again:
5
S = 2.283333333333333
Leave a Reply
You must be logged in to post a comment.