搜索

日间循环练习

Dec 07, 2024 am 09:30 AM

Day - Looping exercises

这里有一些针对数字的 while 循环问题供练习:

基本问题

1.打印数字
编写一个程序,使用 while 循环打印从 1 到 10 的数字。

def print_number(no):
    num=1
    while num





<pre class="brush:php;toolbar:false">1 2 3 4 5 6 7 8 9 10

2.N 个数字的和
编写一个程序,使用 while 循环计算前 NN 个自然数的和。

def sum_of_number(no):
    num=1
    total=0
    while num





<pre class="brush:php;toolbar:false">Sum of the number:10
55

3.偶数
编写一个程序,使用 while 循环打印 1 到 50 之间的所有偶数。

def print_even_number(no):
    num=2
    while num





<pre class="brush:php;toolbar:false">2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 

4.奇数
编写一个程序来打印 1 到 NN 之间的所有奇数。

def print_odd_number(no):
    num=1
    while num





<pre class="brush:php;toolbar:false">Enter the number:20
1 3 5 7 9 11 13 15 17 19

5.倒数
编写一个程序,使用 while 循环以相反的顺序打印从 20 到 1 的数字。

def print_reverse_number(no):
    num=20
    while num>=no:
        print(num, end=" ")
        num-=1
print_reverse_number(1)
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 

中级问题

1.阶乘计算
编写一个程序,使用 while 循环计算给定数字的阶乘。

def find_factorial(num):
    no=1
    factorial=1
    while no





<pre class="brush:php;toolbar:false">Enter the number:5
120
  1. 数字总和 编写一个程序来计算给定数字的数字之和(例如,123 → 1 2 3=6)。
def sum_of_digits(num):
    sum=0
    while num>0:
        sum=sum+num%10
        num=num//10
    return sum
num=int(input("Enter the number:"))
print(sum_of_digits(num))
Enter the number:123
6

3.数数字
编写一个程序来计算给定数字的位数(例如,12345 → 5 位数字)。

def count_of_digits(num):
    count=0
    while num>0:
        num=num//10
        count+=1
    return count
num=int(input("Enter the number:"))
print(count_of_digits(num))

Enter the number:12345
5

4.反转数字
编写一个程序来反转给定的数字(例如 123 → 321)。

def reverse_number(num):
    reverse=0
    while num>0:
        reverse=reverse*10+num%10
        num=num//10
    return reverse
num=int(input("Enter the number:"))
print(reverse_number(num))
Enter the number:123
321

5.乘法表
编写一个程序,使用 while 循环打印给定数字 nn 的乘法表。

def multiply(num):
    no=1
    while no





<pre class="brush:php;toolbar:false">Enter the number:12
1 * 12 = 12
2 * 12 = 24
3 * 12 = 36
4 * 12 = 48
5 * 12 = 60
6 * 12 = 72
7 * 12 = 84
8 * 12 = 96
9 * 12 = 108
10 * 12 = 120
11 * 12 = 132
12 * 12 = 144
13 * 12 = 156
14 * 12 = 168
15 * 12 = 180

高级问题

1.检查回文
编写一个程序来检查给定的数字是否是回文(例如,121→回文,123→不是回文)。

def palindrome(num):
    count=0
    while num>0:
        count=count*10+num%10
        num=num//10
    return count
num=int(input("Enter the number:"))
result=palindrome(num)
if result==num:
    print("Palindrome")
else:
    print("Not palindrome")

Enter the number:121
Palindrome
Enter the number:123
Not palindrome

*2.找到力量*

def find_power(base,power):
    result=1
    while power>=1:
        result=result*base
        power-=1
    return result
base=int(input("Enter the base number:"))
power=int(input("Enter the power number:"))
result=find_power(base,power)
print(result)

Enter the base number:2
Enter the power number:5
32

3.阿姆斯特朗数
编写一个程序来检查给定的数字是否是阿姆斯特朗数(例如,153 → 13 53 33=15313 53 33=153)。

def count_of_digits(num):
    count=0
    while num>0:
        num=num//10
        count+=1
    return count

def find_power(base,power):
    result=1
    while power>=1:
        result=result*base
        power-=1
    return result

def find_armstrong(num,count):
    armstrong=0
    while num>0:
        rem=num%10
        result= find_power(rem,count)
        armstrong=armstrong+result
        num=num//10
    return armstrong


num=int(input("Enter the number:"))
count=count_of_digits(num)
armstrong_result=find_armstrong(num,count)

if armstrong_result==num:
    print("Armstrong")
else:
    print("Not armstrong")
Enter the number:123
Not armstrong
Enter the number:153
Armstrong

4.偶数和奇数位置之和:

def sum_of_even_odd(num):
    odd=0
    even=0
    index=0
    while index<len digit="int(num[index])" if index even="even+digit" else: odd="odd+digit" return num='input("Enter' the number: print of>





<pre class="brush:php;toolbar:false">Enter the number:12345
sum of even number: 9
sum of odd number: 6

以上是日间循环练习的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
什么是Python Switch语句?什么是Python Switch语句?Apr 30, 2025 pm 02:08 PM

本文讨论了Python版本3.10中介绍的新“匹配”语句,该语句与其他语言相同。它增强了代码的可读性,并为传统的if-elif-el提供了性能优势

Python中有什么例外组?Python中有什么例外组?Apr 30, 2025 pm 02:07 PM

Python 3.11中的异常组允许同时处理多个异常,从而改善了并发场景和复杂操作中的错误管理。

Python中的功能注释是什么?Python中的功能注释是什么?Apr 30, 2025 pm 02:06 PM

Python中的功能注释将元数据添加到函数中,以进行类型检查,文档和IDE支持。它们增强了代码的可读性,维护,并且在API开发,数据科学和图书馆创建中至关重要。

Python的单位测试是什么?Python的单位测试是什么?Apr 30, 2025 pm 02:05 PM

本文讨论了Python中的单位测试,其好处以及如何有效编写它们。它突出显示了诸如UNITSEST和PYTEST等工具进行测试。

Python中的访问说明符是什么?Python中的访问说明符是什么?Apr 30, 2025 pm 02:03 PM

文章讨论了Python中的访问说明符,这些说明符使用命名惯例表明班级成员的可见性,而不是严格的执法。

Python中的__Init __()是什么?自我如何在其中发挥作用?Python中的__Init __()是什么?自我如何在其中发挥作用?Apr 30, 2025 pm 02:02 PM

文章讨论了Python的\ _ \ _ Init \ _ \ _()方法和Self在初始化对象属性中的作用。还涵盖了其他类方法和继承对\ _ \ _ Init \ _ \ _()的影响。

python中的@classmethod,@staticmethod和实例方法有什么区别?python中的@classmethod,@staticmethod和实例方法有什么区别?Apr 30, 2025 pm 02:01 PM

本文讨论了python中@classmethod,@staticmethod和实例方法之间的差异,详细介绍了它们的属性,用例和好处。它说明了如何根据所需功能选择正确的方法类型和DA

您如何将元素附加到Python数组?您如何将元素附加到Python数组?Apr 30, 2025 am 12:19 AM

Inpython,YouAppendElementStoAlistusingTheAppend()方法。1)useappend()forsingleelements:my_list.append(4).2)useextend()orextend()或= formultiplelements:my_list.extend.extend(emote_list)ormy_list = [4,5,6] .3)useInsert()forspefificpositions:my_list.insert(1,5).beaware

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具