#include <iostream>
using namespace std;
int main () {
int a, b;
cin>>a>>b;
cout<<a+b<<endl;
cout<<a*b<<endl;
return 0;
}
2. 조건문 - if/else문제: 사용자로부터 점수(0~100)를 입력받고, 해당 점수에 따라 학점을 출력하는 프로그램을 작성하세요.- 90 이상: A- 80 이상 90 미만: B- 70 이상 80 미만: C- 60 이상 70 미만: D- 60 미만: F
#include <iostream>
using namespace std;
int main () {
int score;
cin>>score;
if (score <60) {
cout<<"F"<<endl;
}
else if (score >=60 && score<70) {
cout<<"D"<<endl;
}
else if (score >=70 && score<80) {
cout<<"C"<<endl;
}
else if (score >=80 && score<90) {
cout<<"B"<<endl;
}
else if (score >=90) {
cout<<"A"<<endl;
}
return 0;
}
3. 반복문 - for문문제: 1부터 100까지의 숫자 중에서 3의 배수만 출력하는 프로그램을 작성하세요.
#include <iostream>
using namespace std;
int main () {
for (int i=1; i<=100; i++) {
if (i%3==0) {
cout<<i<<" ";
}
}
return 0;
}
4. 반복문 - while문문제: 사용자가 0을 입력할 때까지 계속해서 숫자를 입력받고, 입력받은 숫자의 합을 출력하는 프로그램을 작성하세요.
#include <iostream>
using namespace std;
int main () {
int sum=0;
int a;
cin>>a;
while ( a!=0) {
sum +=a;
cin>>a;
}
cout<<sum;
return 0;
}
5. 함수 정의와 호출문제: 두 개의 정수를 입력받아 그 합을 반환하는 함수 add(int a, int b)를 작성하고, 이 함수를 이용해 두 수의 합을 출력하는 프로그램을 작성하세요.
#include <iostream>
using namespace std;
int add (int a, int b) {
return a+b;
}
int main () {
int sum=0, a, b;
cin>>a>>b;
sum= add(a, b);
cout<<sum;
return 0;
}
#include <iostream>
using namespace std;
int add() {
int a, b;
cin>>a>>b;
return a+b;
}
int main () {
int sum=add();
cout<<sum;
return 0;
}
6. 배열 사용하기문제: 5명의 학생의 점수를 저장할 수 있는 배열을 선언하고, 사용자로부터 5개의 점수를 입력받아 평균을 계산하여 출력하는 프로그램을 작성하세요.
#include <iostream>
using namespace std;
int main () {
int arr[5];
double sum=0;
for (int i=0; i<5; i++){
cin>>arr[i];
sum+=arr[i];
}
cout<<sum/5;
return 0;
}
7. 문자열 처리문제: 사용자로부터 문자열을 입력받아 그 문자열이 회문(앞뒤가 같은 문자열)인지 아닌지를 판단하는 프로그램을 작성하세요.
#include <iostream>
#include <string>
using namespace std;
int main() {
int n;
cin>>n;
string a[n];
for (int i=0; i<=n; i++){
cin>>a[i];
}
for (int i=0; i<=n; i++) {
if (a[i]==a[n-i]) {
cout<<"True";
}
else {
cout<<"False";
}
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cin >> str; // 사용자로부터 문자열 입력받기
int len = str.length(); // 문자열의 길이 //왜?
// 문자열의 앞과 뒤를 비교
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) {
cout<<"False";
}
else {
cout<<"True";
}
break;
}
return 0;
}
8. 클래스와 객체문제: Rectangle이라는 클래스를 정의하세요.
이 클래스는 가로와 세로 길이를 멤버 변수로 가지고, 면적을 계산하는 area() 멤버 함수를 포함해야 합니다. Rectangle 객체를 생성하고 면적을 계산하는 프로그램을 작성하세요.
#include <iostream>
using namespace std;
class Rectangle {
public:
int width;
int height;
Rectangle (int w, int h) : width (w), height(h) {} //매개변수, 멤버변수 초기화
int area (){
return width*height;
}
};
int main() {
int width, height;
cin>>width>> height;
int s;
Rectangle rectangle(width, height);
s= rectangle.area();
cout<<s;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cin>>a;
int *p= &a;
cin>>b;
*p= b;
cout<<a;
return 0;
}
3. 반복문문제: 다음 중 for문이 올바르게 사용된 예는 무엇인가요?
4. 함수문제: 다음 중 함수 오버로딩의 정의로 올바른 것은 무엇인가요?
5. 클래스와 객체문제: 다음 중 C++에서 클래스에 대한 설명으로 올바른 것은 무엇인가요?