데일리 codeup/출력

출력형식_220701

hayo_su 2022. 7. 1. 12:31

c++에서는 cout 을 이용하여 순차적으로 << 뒤에 변수들을 써주면 차례로 출력할 수 있다.
문자열인 string 자료형을 이용하기 위해서는 string 헤더를 코드 #include <string> 를 상단에 포함시켜줘야 한다.

예제


#include <iostream>
#include <string>
using namespace std;

int main() {

    int a = 5;
    cout << "A is " << a << endl;

    string b = "apple";
    cout << "B is " << b << endl;

    cout << "A is " << a << " and B is " << b;

    return 0;

}

출력 결과

A is 5
B is apple
A is 5 and B is apple

세 정수형 변수 선언

세 정수형 변수를 선언하고 차례로 7, 23, 30 을 넣어 덧셈식을 출력합니다.

#include <iostream>
using namespace std;
int main(){
    int a, b, c;
    a = 7;
    b = 23;
    c = 30;
    cout << a << " + " << b << " = "<< c;
    return 0;
}

출력결과

7 + 23 = 30

변수 출력하기

변수 a, b에 각각 3, 'C'을 넣어주고, 출력 형식에 알맞게 출력하는 프로그램을 작성하세요.

#include <iostream>
#include <string>
using namespace std;
int main(){
    int a = 3;
    string b = "C";
    cout << a << "..."<<b;
    return 0;

}

출력결과

3...C

변수 출력하기 2

변수 a, b에 각각 3, 'C'을 넣어주고, 출력 형식에 알맞게 출력하는 프로그램을 작성하세요.

#include <iostream>
#include <string>
using namespace std;
int main(){
    int a = 3;
    string b = "C";
    cout << b << "!.....!" << a;
    return 0;
}

출력결과

C!.....!3

변수 출력하기3

변수 a, b, c에 각각 1, 2, 'C'을 넣어주고, 출력 형식에 알맞게 출력하는 프로그램을 작성하세요.

#include <iostream>
#include <string>
using namespace std;
int main(){
    int a = 1;
    int b = 2;
    string c = "C";
    cout << a << "->" << b << "->" << c;
    return 0;
}

출력 결과

1->2->C