打印所有不能被 2 或 3 整除且介于 1 和 50 之间的整数的 Python 程序

当需要打印所有不能被 2 或 3 整除且介于 1 和 50 之间的元素时,以“while”循环和“if”条件的形式提及约束。

以下是相同的演示 -

示例

print("Integers not divisible by 2 and 3, that lie between 1 and 50 are : ")

n = 1

while n <= 51:

   if n % 2 != 0 and n % 3 != 0:

      print(n)

   n = n+1

输出结果
Integers not divisible by 2 and 3, that lie between 1 and 50 are :

1

5

7

11

13

17

19

23

25

29

31

35

37

41

43

47

49

解释

  • n 的值被赋值为 1。

  • while 循环一直运行,直到此 'n' 不大于 51,

  • 它检查数字是否可以被 2 或 3 整除。

  • 如果不能整除,则在控制台上显示该数字。

  • 每次显示后,它都会增加。

以上是 打印所有不能被 2 或 3 整除且介于 1 和 50 之间的整数的 Python 程序 的全部内容, 来源链接: utcz.com/z/317400.html

回到顶部