sol include iostream using namespace std int main

  • Slides: 35
Download presentation

sol #include <iostream> using namespace std; int main() { int a, b; cout<<“정수(2개): ”;

sol #include <iostream> using namespace std; int main() { int a, b; cout<<“정수(2개): ”; cin>>a>>b; if(a>b) { cout<<“큰수: ”<<a<<endl; }else{ cout<<“큰수: ”<<b<<endl; } return 0; } 11

sol #include <iostream> using namespace std; int main() { int a, b; cout<<“정수(2개): ”;

sol #include <iostream> using namespace std; int main() { int a, b; cout<<“정수(2개): ”; cin>>a>>b; if(a<0) { cout<<“음수다: ”<<endl; }else if(a>0){ cout<<“양수다: ”<<endl; }else{ cout<<“ 0이다: ”<<endl; } return 0; } 13

sol #include <iostream> using namespace std; int main() { int a, b; cout<<“정수(3개): ”;

sol #include <iostream> using namespace std; int main() { int a, b; cout<<“정수(3개): ”; cin>>a>>b>>c; if(a>b) { if(a>c) cout<<“가장 큰 수: ”<<a<<endl; else cout<<“가장 큰 수: ”<<c<<endl; } else { if(b>c) cout<<“가장 큰 수: ”<<b<<endl; else cout<<“가장 큰 수: ”<<c<<endl; } return 0; } 15

sol #include <iostream> using namespace std; int main() { int a; cout<<“정수: ”; cin>>a;

sol #include <iostream> using namespace std; int main() { int a; cout<<“정수: ”; cin>>a; if(a%2==0) { cout<<“짝수: ”<<endl; }else{ cout<<“홀수: ”<<endl; } return 0; } 17

sol # include <iostream> using namespace std; int main() { int a; cout<<“정수 입력하세요:

sol # include <iostream> using namespace std; int main() { int a; cout<<“정수 입력하세요: ”; cin>>a; if(a%3==0&&a%9==0){ cout<<a<<“는 3의 배수이면서 9의 배수입니다. ”<<endl; } else{ cout<<a<<“는 3의 배수이면서 9의 배수가 아닙니다. ”<<endl; } return 0; } 20

24

24

25

25

선택문 l if ~ else if ~ - else 블록 중에서 다시 선택 다중선택

선택문 l if ~ else if ~ - else 블록 중에서 다시 선택 다중선택 #include <stdio. h> int main() { float weight, height , bmi; cout<<"몸무게(kg) , 키cm) 입력: "; cin>>weight >>height; if(bmi>=20. 0 && bmi<25. 0) cout<<“표준체중입니다. ₩n”; else if ( bmi<20 ) cout<<“저체중입니다. ₩n”; else cout<<“과체중입니다. ₩n”; height = height / 100; bmi = weight / (height * height); cout<<"당신의 비만도는 : ”<<bmi<<“입니다. ₩n"; if(bmi>=20. 0 && bmi<25. 0) cout<<"표준체중 입니다. ₩n"; else cout<<"체중관리가 필요합니다. ₩n"; return 0; } 28

선택문 연습(윤년) l 4자리 년도를 입력하여 윤년인지 판단 코딩 § #include <iostream> using namespace

선택문 연습(윤년) l 4자리 년도를 입력하여 윤년인지 판단 코딩 § #include <iostream> using namespace std; int main() { int year, r 4, r 100, r 400; cout<<“년도 4자리 입력: "; cin>>year; r 4 = year%4; r 100 = year%100; r 400 = year%400; if( (r 4==0&&r 100!=0) || r 400==0 ) cout<<year<<“ 년은 윤년입니다. ”<<endl; else cout<<year<<“ 년은 평년입니다. ”<<endl; return 0; } 32

선택문 l 다중선택(switch~case) • 정수를 입력하여 정 수값에 따라 당첨자를 결정하는 프로그램 작 성

선택문 l 다중선택(switch~case) • 정수를 입력하여 정 수값에 따라 당첨자를 결정하는 프로그램 작 성 하기 #include <iostream> using namespace std; int main() { int lot; cout<<"당첨 번호를 입력하세요 : "; cin>>lot; switch(lot) { case 1: cout<<"김서방"; break; case 2: cout<<"홍가네"; break; case 3: cout<<"안산댁"; break; default: cout<<"당첨자가 없습니다!"; } return 0; } 34