찾다

일 - 반복 연습

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으로 문의하세요.
파이썬 어레이를 어떻게 슬라이스합니까?파이썬 어레이를 어떻게 슬라이스합니까?May 01, 2025 am 12:18 AM

Python List 슬라이싱의 기본 구문은 목록 [start : stop : step]입니다. 1. Start는 첫 번째 요소 인덱스, 2.Stop은 첫 번째 요소 인덱스가 제외되고 3. Step은 요소 사이의 단계 크기를 결정합니다. 슬라이스는 데이터를 추출하는 데 사용될뿐만 아니라 목록을 수정하고 반전시키는 데 사용됩니다.

어떤 상황에서 목록이 배열보다 더 잘 수행 될 수 있습니까?어떤 상황에서 목록이 배열보다 더 잘 수행 될 수 있습니까?May 01, 2025 am 12:06 AM

ListSoutPerformArraysin : 1) DynamicsizingandFrequentInsertions/Deletions, 2) StoringHeterogeneousData 및 3) MemoryEfficiencyForsParsEdata, butMayHavesLightPerformanceCosceperationOperations.

파이썬 어레이를 파이썬 목록으로 어떻게 변환 할 수 있습니까?파이썬 어레이를 파이썬 목록으로 어떻게 변환 할 수 있습니까?May 01, 2025 am 12:05 AM

TOCONVERTAPYTHONARRAYTOALIST, USETHELIST () CONSTUCTORORAGENERATERATOREXPRESSION.1) importTheArrayModuleAndCreateAnarray.2) USELIST (ARR) 또는 [XFORXINARR] TOCONVERTITTOALIST.

Python에 목록이있을 때 배열을 사용하는 목적은 무엇입니까?Python에 목록이있을 때 배열을 사용하는 목적은 무엇입니까?May 01, 2025 am 12:04 AM

chooSearRaysOverListSinpyTonforBetTerferformanceAndMemoryEfficiencyInspecificscenarios.1) arrgenumericalDatasets : arraysreducememoryUsage.2) Performance-CriticalOperations : ArraysofferspeedboostsfortaskslikeApenorsearching.3) TypeSenforc

목록과 배열의 요소를 반복하는 방법을 설명하십시오.목록과 배열의 요소를 반복하는 방법을 설명하십시오.May 01, 2025 am 12:01 AM

파이썬에서는 루프에 사용하여 열거 및 추적 목록에 대한 이해를 나열 할 수 있습니다. Java에서는 루프를 위해 전통적인 사용 및 루프가 트래버스 어레이를 향해 향상시킬 수 있습니다. 1. Python 목록 트래버스 방법에는 다음이 포함됩니다. 루프, 열거 및 목록 이해력. 2. Java 어레이 트래버스 방법에는 다음이 포함됩니다. 루프 용 전통 및 루프를위한 향상.

Python Switch 문은 무엇입니까?Python Switch 문은 무엇입니까?Apr 30, 2025 pm 02:08 PM

이 기사는 버전 3.10에 도입 된 Python의 새로운 "매치"진술에 대해 논의하며, 이는 다른 언어로 된 문장과 동등한 역할을합니다. 코드 가독성을 향상시키고 기존 IF-ELIF-EL보다 성능 이점을 제공합니다.

파이썬의 예외 그룹은 무엇입니까?파이썬의 예외 그룹은 무엇입니까?Apr 30, 2025 pm 02:07 PM

Python 3.11의 예외 그룹은 여러 예외를 동시에 처리하여 동시 시나리오 및 복잡한 작업에서 오류 관리를 향상시킵니다.

파이썬의 기능 주석이란 무엇입니까?파이썬의 기능 주석이란 무엇입니까?Apr 30, 2025 pm 02:06 PM

Python의 기능 주석은 유형 확인, 문서 및 IDE 지원에 대한 기능에 메타 데이터를 추가합니다. 코드 가독성, 유지 보수를 향상 시키며 API 개발, 데이터 과학 및 라이브러리 생성에 중요합니다.

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

SublimeText3 Linux 새 버전

SublimeText3 Linux 새 버전

SublimeText3 Linux 최신 버전

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경