과제

컴사파 - 2주차

とおき 2022. 9. 17. 01:22

2.1 원의 반지름 입력, 넓이와 원둘레 계산 및 출력

소스코드

# 22211975_homework2
"""
 Project: 22211975_homework2.1
 Author: 
 Date of last update: 22.09.08
 Update list:
    v1.0: 22.09.08
     - added source code
"""
pi = 3.14
r = int(input("원의 반지름: ")) # integer variable
c_area = pi * r * r # formula of circle-area
c_circumference = 2 * pi * r # formula of circle-circumference
print(f"넓이: {c_area}, 둘레: {c_circumference}")

실행 결과

 

2.2 직사각형의 가로, 세로 입력, 넓이와 둘레 계산 및 출력

소스코드

# 22211975_homework2
"""
 Project: 22211975_homework2.2
 Author:
 Date of last update: 22.09.08
 Update list:
     -v1.0: 22.09.08
         - added source code
"""

# definitions of variables
width = int(input("직사각형의 세로길이: ")) # integer variable
length = int(input("직사각형의 가로길이: ")) # integer variable

r_area = width * length
r_circumference = 2 * (width + length)
print("넓이: {}, 둘레: {}".format(r_area, r_circumference))

실행결과

 

2.3 0 ~ 255값의 10진수, 2진수, 8진수 및 16진수 출력, 출력 포멧 설정, 접두어 출력

소스코드

# 22211975_homework2
"""
 Project: 22211975_homework2.3
 Author:
 Date of last update: 22.09.08
 Update list:
     -v1.0: 22.09.08
         - added source code
"""
# definitions of variables
num = int(input("0~255 사이의 숫자를 입력: ")) # integer variable
num_bin = '{:#b}'.format(num) # 10진수 -> 2진수
num_oct = '{:#o}'.format(num) # 10진수 -> 8진수
num_hex = '{:#x}'.format(num) # 10진수 -> 16진수

print("10진수: {}\n2진수: {}\n8진수: {}\n16진수: {}\n".format(num, num_bin, num_oct, num_hex))

실행결과