Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

최대 세 개의 숫자를 찾는 Python 프로그램

<시간/>

이 튜토리얼에서는 세 개의 숫자에서 최대값을 찾는 프로그램을 작성할 것입니다. 우리는 3개의 숫자를 갖게 될 것이고 우리의 목표는 이 3개의 숫자에서 최대 숫자를 찾는 것입니다.

더 나은 이해를 위해 몇 가지 샘플 테스트 사례를 살펴보겠습니다.

Input:
a, b, c = 2, 34, 4
Output:
34


Input:
a, b, c = 25, 3, 12
Output:
25


Input:
a, b, c = 5, 5, 5
Output:
5

세 숫자 중 최대 숫자를 찾으려면 아래 단계를 따르세요.

알고리즘

1. Initialise three numbers a, b, c.
2. If a is higher than b and c then, print a.
3. Else if b is greater than c and a then, print b.
4. Else if c is greater than a and b then, print c.
5. Else print any number.

위의 알고리즘에 대한 코드를 보자.

## initializing three numbers
a, b, c = 2, 34, 4
## writing conditions to find out max number
## condition for a
if a > b and a > c:
   ## printing a
   print(f"Maximum is {a}")
## condition for b
elif b > c and b > a:
   ## printing b
   print(f"Maximum is {b}")
## condition for c
elif c > a and c > b:
   ## printing
   print(f"Maximum is {c}")
## equality case
else:
   ## printing any number among three
   print(a)

출력

위의 프로그램을 실행하면 다음과 같은 결과를 얻을 수 있습니다.

Maximum is 34

다른 테스트 케이스에 대해 코드를 다시 한 번 실행해 보겠습니다.

## initializing three numbers
a, b, c = 5, 5, 5
## writing conditions to find out max number
## condition for a
if a > b and a > c:
   ## printing a
   print(f"Maximum is {a}")
## condition for b
elif b > c and b > a:
   ## printing b
   print(f"Maximum is {b}")
## condition for c
elif c > a and c > b:
   ## printing
   print(f"Maximum is {c}")
## equality case
else:
   ## printing any number among three
   print(f"Maximum is {a}")

출력

위의 프로그램을 실행하면 다음과 같은 결과를 얻을 수 있습니다.

Maximum is 5

결론

튜토리얼과 관련하여 궁금한 점이 있으면 댓글 섹션에 언급하세요.