소개
이 랩에서는 C++ 프로그래밍 언어로 두 숫자를 더하는 기본적인 프로그램을 작성하는 방법을 배우게 됩니다. 또한 C++ 에서 변수 선언, 초기화, 입출력 연산 및 기본적인 수학 연산에 대해서도 배우게 됩니다.
이 랩에서는 C++ 프로그래밍 언어로 두 숫자를 더하는 기본적인 프로그램을 작성하는 방법을 배우게 됩니다. 또한 C++ 에서 변수 선언, 초기화, 입출력 연산 및 기본적인 수학 연산에 대해서도 배우게 됩니다.
먼저, 사용자 입력을 저장하기 위해 정수형 변수 a와 b를 선언합니다. 또한 sum 변수를 선언하고 0 으로 초기화합니다.
#include<iostream>
using namespace std;
int main()
{
//variable declaration
int a, b;
//variable declaration and initialization
int sum=0;
//...
}
cin과 cout 문을 사용하여 사용자로부터 입력을 받습니다. cout는 사용자에게 메시지를 표시하는 데 사용되고, cin은 사용자로부터 입력을 받는 데 사용됩니다.
cout << "\n\nWelcome to LabEx :-)\n\n\n";
cout << " ===== Program to add 2 numbers ===== \n\n";
cout << "Enter the first Number : ";
cin >> a;
cout << "\nEnter the second Number : ";
cin >> b;
두 숫자 a와 b를 더하고 결과를 변수 sum에 저장합니다.
//Adding the two numbers
sum = a + b;
cout 문을 사용하여 사용자에게 결과를 표시합니다.
//displaying the final output (sum)
cout << "\nAddition of the two numbers is : " << sum;
터미널에서 다음 명령을 사용하여 프로그램을 컴파일합니다.
g++ ~/project/main.cpp -o main
터미널에서 다음 명령을 사용하여 프로그램을 실행합니다.
./main
프로그램을 실행한 후 터미널에서 다음 출력을 확인해야 합니다.
Welcome to LabEx :-)
===== Program to add 2 numbers =====
Enter the first Number : 10
Enter the second Number : 20
Addition of the two numbers is : 30
이 랩에서는 두 숫자를 더하는 기본적인 C++ 프로그램을 작성하는 방법을 배웠습니다. 또한 C++ 에서 변수 선언 (variable declaration), 초기화 (initialization), 사용자 입출력 (user input/output), 그리고 기본적인 수학 연산에 대해 배웠습니다. 이제 다른 수학 연산을 시도하거나 필요에 맞게 프로그램을 수정할 수 있습니다.
다음은 main.cpp 파일의 전체 코드입니다.
#include<iostream>
using namespace std;
int main()
{
//variable declaration
int a, b;
//variable declaration and initialization
int sum=0;
//take user input
cout << "\n\nWelcome to LabEx :-)\n\n\n";
cout << " ===== Program to add 2 numbers ===== \n\n";
cout << "Enter the first Number : ";
cin >> a;
cout << "\nEnter the second Number : ";
cin >> b;
//Adding the two numbers
sum = a + b;
//displaying the final output (sum)
cout << "\nAddition of the two numbers is : " << sum;
return 0;
}