직사각형 넓이 구하기
직사각형 넓이 구하기는 프로그래밍을 배우는 데 있어 기본적인 문제 중 하나입니다. 이 예제에서는 사용자로부터 직사각형의 가로와 세로 길이를 입력받아 넓이를 계산해 출력하는 프로그램을 작성하게 됩니다. 가로와 세로 길이를 곱하여 직사각형의 넓이를 구하는 간단한 공식을 이용하여, 프로그래밍 언어에 따른 구현 방식을 이해하고 익히는 데 도움이 됩니다. 이 예제를 통해 다양한 프로그래밍 언어의 입출력 처리, 변수 선언 및 할당, 그리고 함수 호출 방식 등을 살펴볼 수 있습니다.
예제: 직사각형 넓이 구하기
코드: RectangleArea.cs
using System;
class RectangleArea
{
// 직사각형의 넓이를 계산하는 함수
static int CalcRectangleArea(int width, int height)
{
return width * height; // 가로 * 세로
}
static void Main()
{
int width, height;
// 사용자로부터 직사각형의 가로와 세로 길이를 입력받음
Console.Write("직사각형의 가로와 세로 길이를 입력하세요:\n");
width = Convert.ToInt32(Console.ReadLine());
height = Convert.ToInt32(Console.ReadLine());
// 계산된 넓이를 출력
Console.WriteLine("직사각형의 넓이: {0}", CalcRectangleArea(width, height));
}
}
직사각형의 가로와 세로 길이를 입력하세요:
10
5
직사각형의 넓이: 50
예제: 직사각형 넓이 구하기
코드: rectangle_area.c
#define _CRT_SECURE_NO_WARNINGS // Visual Studio에서 안전하지 않은 함수 경고를 무시하도록 함
#include <stdio.h>
// 직사각형의 넓이를 계산하는 함수
int rectangle_area(int width, int height)
{
return width * height; // 가로 * 세로
}
int main(void)
{
int width, height;
// 사용자로부터 직사각형의 가로와 세로 길이를 입력받음
printf("직사각형의 가로와 세로 길이를 입력하세요: ");
scanf("%d %d", &width, &height);
// 계산된 넓이를 출력
printf("직사각형의 넓이: %d\n", rectangle_area(width, height));
return 0;
}
직사각형의 가로와 세로 길이를 입력하세요: 10 5
직사각형의 넓이: 50
예제: 직사각형 넓이 구하기
코드: RectangleArea.java
import java.util.Scanner;
public class RectangleArea {
// 직사각형의 넓이를 계산하는 함수
public static int rectangleArea(int width, int height) {
return width * height; // 가로 * 세로
}
public static void main(String[] args) {
int width, height;
// 사용자로부터 직사각형의 가로와 세로 길이를 입력받음
Scanner scanner = new Scanner(System.in);
System.out.print("직사각형의 가로와 세로 길이를 입력하세요: ");
width = scanner.nextInt();
height = scanner.nextInt();
scanner.close();
// 계산된 넓이를 출력
System.out.printf("직사각형의 넓이: %d\n", rectangleArea(width, height));
}
}
직사각형의 가로와 세로 길이를 입력하세요: 10 5
직사각형의 넓이: 50
예제: 직사각형 넓이 구하기
코드: rectangle_area.py
def rectangle_area(width, height):
"""
직사각형의 넓이를 계산하는 함수
"""
return width * height # 가로 * 세로
if __name__ == '__main__':
# 사용자로부터 직사각형의 가로와 세로 길이를 입력받음
width, height = map(int, input("직사각형의 가로와 세로 길이를 입력하세요: ").split())
# 계산된 넓이를 출력
print("직사각형의 넓이: {}".format(rectangle_area(width, height)))
직사각형의 가로와 세로 길이를 입력하세요: 10 5
직사각형의 넓이: 50
예제: 직사각형 넓이 구하기
코드: rectangle_area.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>직사각형 넓이 구하기</title>
<script>
function rectangle_area(width, height) {
// 직사각형의 넓이를 계산하는 함수
return width * height; // 가로 * 세로
}
function calculateArea() {
// 사용자로부터 직사각형의 가로와 세로 길이를 입력받음
var width = parseInt(document.getElementById("width").value);
var height = parseInt(document.getElementById("height").value);
// 계산된 넓이를 출력
document.getElementById("result").innerHTML = "직사각형의 넓이: " + rectangle_area(width, height);
}
</script>
</head>
<body>
<h1>직사각형 넓이 구하기</h1>
<p>직사각형의 가로와 세로 길이를 입력하세요:</p>
<label for="width">가로: </label>
<input type="number" id="width">
<br>
<label for="height">세로: </label>
<input type="number" id="height">
<br><br>
<button onclick="calculateArea()">넓이 계산</button>
<p id="result"></p>
</body>
</html>
직사각형의 가로와 세로 길이를 입력하세요: 10 5
직사각형의 넓이: 50
예제: 직사각형 넓이 구하기
코드: RectangleArea.cpp
#include <iostream>
// 직사각형의 넓이를 계산하는 함수
int rectangle_area(int width, int height) {
return width * height; // 가로 * 세로
}
int main() {
int width, height;
// 사용자로부터 직사각형의 가로와 세로 길이를 입력받음
std::cout << "직사각형의 가로와 세로 길이를 입력하세요: ";
std::cin >> width >> height;
// 계산된 넓이를 출력
std::cout << "직사각형의 넓이: " << rectangle_area(width, height) << std::endl;
return 0;
}
직사각형의 가로와 세로 길이를 입력하세요: 10 5
직사각형의 넓이: 50
예제: 직사각형 넓이 구하기
코드: rectangle_area.go
package main
import "fmt"
// 직사각형의 넓이를 계산하는 함수
func rectangleArea(width int, height int) int {
return width * height // 가로 * 세로
}
func main() {
var width, height int
// 사용자로부터 직사각형의 가로와 세로 길이를 입력받음
fmt.Print("직사각형의 가로와 세로 길이를 입력하세요: ")
fmt.Scanf("%d %d", &width, &height)
// 계산된 넓이를 출력
fmt.Printf("직사각형의 넓이: %d\n", rectangleArea(width, height))
}
직사각형의 가로와 세로 길이를 입력하세요: 10 5
직사각형의 넓이: 50
예제: 직사각형 넓이 구하기
코드: rectangle_area.rs
use std::io;
// 직사각형의 넓이를 계산하는 함수
fn rectangle_area(width: i32, height: i32) -> i32 {
width * height // 가로 * 세로
}
fn main() {
let mut width = String::new();
let mut height = String::new();
// 사용자로부터 직사각형의 가로와 세로 길이를 입력받음
println!("직사각형의 가로와 세로 길이를 입력하세요: ");
io::stdin().read_line(&mut width).unwrap();
io::stdin().read_line(&mut height).unwrap();
let width: i32 = width.trim().parse().unwrap();
let height: i32 = height.trim().parse().unwrap();
// 계산된 넓이를 출력
println!("직사각형의 넓이: {}", rectangle_area(width, height));
}
직사각형의 가로와 세로 길이를 입력하세요:
10
5
직사각형의 넓이: 50
예제: 직사각형 넓이 구하기
코드: rectangle_area.ts
// 직사각형의 넓이를 계산하는 함수
function rectangleArea(width: number, height: number): number {
return width * height; // 가로 * 세로
}
function main() {
const width: number = parseInt(prompt("직사각형의 가로와 세로 길이를 입력하세요: ") as string, 10);
const height: number = parseInt(prompt("직사각형의 가로와 세로 길이를 입력하세요: ") as string, 10);
// 계산된 넓이를 출력
console.log("직사각형의 넓이:", rectangleArea(width, height));
}
main();
직사각형의 가로와 세로 길이를 입력하세요: 10
직사각형의 가로와 세로 길이를 입력하세요: 5
직사각형의 넓이: 50
예제: 직사각형 넓이 구하기
코드: RectangleArea.kt
import java.util.Scanner
// 직사각형의 넓이를 계산하는 함수
fun rectangleArea(width: Int, height: Int): Int {
return width * height // 가로 * 세로
}
fun main() {
val scanner = Scanner(System.`in`)
var width: Int
var height: Int
// 사용자로부터 직사각형의 가로와 세로 길이를 입력받음
print("직사각형의 가로와 세로 길이를 입력하세요: ")
width = scanner.nextInt()
height = scanner.nextInt()
// 계산된 넓이를 출력
println("직사각형의 넓이: ${rectangleArea(width, height)}")
}
직사각형의 가로와 세로 길이를 입력하세요: 10 5
직사각형의 넓이: 50