新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
Python程序:檢查數字是不是Pronic數字
創(chuàng)新互聯(lián)python教程:

網站建設哪家好,找創(chuàng)新互聯(lián)!專注于網頁設計、網站建設、微信開發(fā)、微信小程序定制開發(fā)、集團企業(yè)網站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了廣德免費建站歡迎大家使用!
編寫一個 Python 程序來檢查一個數字是不是 Pronic 數字,或者是否使用 while 循環(huán)。例如,如果一個數等于兩個連續(xù)數的乘積,則它是一個 Pronic 數,這意味著 number = n(n + 1)。
在這個 Python 程序中,I 值從零迭代到該數字的平方根,并檢查任意兩個連續(xù)數字的乘積是否等于實際數字。如果為真,標志值變?yōu)?1,break 語句將退出循環(huán)。如果標志等于 1,則它是一個 Pronic 數。
import math
Number = int(input("Enter the Number to Check Pronic Number = "))
i = 0
flag = 0
while i <= (int) (math.sqrt(Number)):
if Number == i * (i + 1):
flag = 1
break
i = i + 1
if flag == 1:
print("\n%d is a Pronic Number." %Number)
else:
print("%d is Not a Pronic Number." %Number)
Python 程序,使用 for 循環(huán)檢查給定的數字是否是 Pronic 數字。
Number = int(input("Enter the Number to Check Pronic Number = "))
flag = 0
for i in range(Number + 1):
if Number == i * (i + 1):
flag = 1
break
if flag == 1:
print("\n%d is a Pronic Number." %Number)
else:
print("%d is Not a Pronic Number." %Number)
Enter the Number to Check Pronic Number = 42
42 is a Pronic Number.
Enter the Number to Check Pronic Number = 55
55 is Not a Pronic Number.Python 程序查找一個數字是不是 Pronic 數或者不使用函數。
def pronicNumber(Number):
flag = 0
for i in range(Number + 1):
if Number == i * (i + 1):
flag = 1
break
return flag
Number = int(input("Enter the Number to Check Pronic Number = "))
if pronicNumber(Number) == 1:
print("\n%d is a Pronic Number." %Number)
else:
print("%d is Not a Pronic Number." %Number)
Enter the Number to Check Pronic Number = 52
52 is Not a Pronic Number.
Enter the Number to Check Pronic Number = 72
72 is a Pronic Number. 當前題目:Python程序:檢查數字是不是Pronic數字
本文網址:http://www.5511xx.com/article/cdpgiso.html


咨詢
建站咨詢
