#include <iostream>
using namespace std;
int main()
{
// 프로그램 내용
return 0;
}
#include <iostream>: 입출력 스트림 헤더 포함using namespace std;: std 네임스페이스 사용 선언int main(): 프로그램의 시작점return 0;: 정상 종료 신호// 한 줄 주석
/*
여러 줄 주석
블록 주석
*/
int age = 25; // 4바이트
short height = 170; // 2바이트
long population = 50000000L; // 4바이트 이상
float pi = 3.14f; // 4바이트
double precision = 3.141592653; // 8바이트
bool isStudent = true; // 1바이트
bool isWorking = false;
char grade = 'A'; // 1바이트
#include <string>
string name = "홍길동";
string greeting = "Hello" + " " + "World!";//걍 한번에 띄어쓰기있이 붙여써도됨 왜 이따구로햇는지모르겟네?
string number_str = to_string(123); // 숫자를 문자열로 변환
const double PI = 3.141592;
const int MAX_SIZE = 100;
auto number = 42; // int로 추론
auto price = 99.99; // double로 추론
auto name = "김철수"; // const char*로 추론
<< : 삽입 연산자 (insertion operator) 👉 cout << "어쩌구"; = "어쩌구"라는 데이터를 출력 스트림에 집어넣는다 → 화면에 표시됨.>> : 추출 연산자 (extraction operator) 👉 cin >> 변수명; = 입력 스트림에서 데이터를 꺼내서 변수에 저장한다.cout << : "데이터를 파이프(스트림)에 흘려보낸다 → 밖(모니터)로 간다"cin >> : "파이프(스트림)에서 데이터를 꺼낸다 → 내 변수로 가져온다"cout << "문자열 출력";
cout << 변수명;
cout << "값: " << 변수 << endl;
int age;
cin >> age;
string name;
cin >> name; // 공백 전까지만 입력받음
// 여러 값 한번에 입력
int a, b;
cin >> a >> b;
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
cout << "이름을 입력하세요: ";
cin >> name;
cout << "나이를 입력하세요: ";
cin >> age;
cout << name << "님의 나이는 " << age << "세입니다." << endl;
return 0;
}
int a = 10, b = 3;
cout << a + b; // 13 (덧셈)
cout << a - b; // 7 (뺄셈)
cout << a * b; // 30 (곱셈)
cout << a / b; // 3 (나눗셈 - 정수)
cout << a % b; // 1 (나머지)
'bool'은 컴퓨터 프로그래밍에서 참(true)과 거짓(false)이라는 두 가지 값만 저장하는 논리 자료형(boolean type)을 의미하며, 영국 수학자 조지 불(George Boole)의 이름에서 유래했습니다. 이 자료형은 조건문이나 반복문에서 특정 상태를 참 또는 거짓으로 판단하여 제어하는 데 사용됩니다.
int x = 5, y = 3;
bool result1 = (x > y); // true
bool result2 = (x == y); // false
bool result3 = (x != y); // true
#include <ctime>
#include <cstdlib>
srand(time(NULL)); // 시드값 초기화
int dice = (rand() % 6) + 1; // 1~6 사이 난수
C++의 개발자와 개발 장소는?
정답: 2번
다음 빈칸을 채우시오.
_______ <iostream>
_______ namespace std;
int _______()
{
cout << "Hello World!" << _______;
_______ 0;
}
정답:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
다음 변수 선언에서 잘못된 부분을 찾고 올바르게 수정하시오.
int 2number = 100;
char name = "홍길동";
bool isTrue = 1;
const PI = 3.14;
정답:
int number2 = 100; // 변수명은 숫자로 시작할 수 없음
string name = "홍길동"; // 문자열은 string 타입 사용
bool isTrue = true; // bool은 true/false 사용 권장
const double PI = 3.14; // const는 타입 명시 필요
사용자로부터 두 정수를 입력받아 사칙연산 결과를 모두 출력하는 프로그램을 작성하시오.
정답:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "두 정수를 입력하세요: ";
cin >> a >> b;
cout << a << " + " << b << " = " << a + b << endl;
cout << a << " - " << b << " = " << a - b << endl;
cout << a << " * " << b << " = " << a * b << endl;
cout << a << " / " << b << " = " << a / b << endl;
cout << a << " % " << b << " = " << a % b << endl;
return 0;
}
다음 조건을 만족하는 프로그램을 작성하시오:
정답:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const double PI = 3.141592;
double radius;
cout << "원의 반지름을 입력하세요: ";
cin >> radius;
double area = PI * radius * radius;
double circumference = 2 * PI * radius;
cout << fixed << setprecision(2);
cout << "원의 넓이: " << area << endl;
cout << "원의 둘레: " << circumference << endl;
return 0;
}
다음 프로그램의 오류를 찾아 수정하시오.
#include iostream
using namespace std
int main()
{
int age
cout << "나이를 입력하세요: "
cin >> age
cout << "당신의 나이는 " << age << "세입니다." << endl
return 0
}
정답 (수정된 코드):
#include <iostream> // < > 누락
using namespace std; // 세미콜론 누락
int main()
{
int age; // 세미콜론 누락
cout << "나이를 입력하세요: "; // 세미콜론 누락
cin >> age; // 세미콜론 누락
cout << "당신의 나이는 " << age << "세입니다." << endl; // 세미콜론 누락
return 0; // 세미콜론 누락
}