<예제6 >
100에서 1000사이의 모든 숫자에 대해 5와 6으로 나누어 떨어지는 숫자를 찾아 한 줄에 10개씩 출력하는 프로그램 작성
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int i;
int cnt=0;
for(i=100;i<=1000;i++) {
if((i%5==0) && (i%6==0)){
cout<<setw(4)<<i; //100부터 1000사이의 수는 세자리수->setw(4)
cnt++;
if(cnt%10==0)
cout<<"\n";
}
}
return 0;
}
또는,
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int i; int cnt=0;
for(i=100;i<=1000;i++){
if(i%5==0 && i%6==0){
/*if(cnt++ %10 ==0)
cout<<"\n";
else
cout<<setw(4)<<i;
*/
(cnt++ % 10 ==0)) ? cout<<"\n" : cout<<setw(4)<<i; //if-else문 bool식 변환
}
}
return 0;
}
2017년 4월 14일 금요일
while문_정수구하기
while문 사용하여 n*n>12000이 되는 가장 작은 정수 n을 구하여라.
#include <iostream>
using namespace std;
int main() {
int n=0;
while((n*n)<12000){
n++;
}
cout<<"This number is "<<n<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n=0;
while((n*n)<12000){
n++;
}
cout<<"This number is "<<n<<endl;
return 0;
}
bool()이용_삼각형 둘레길이 구하기
#include <iostream>
using namespace std;
int main() {
int edge1, edge2, edge3;
int sum=0;
cout<<"Enter three edges : "; cin>>edge1>>edge2>>edge3;
bool Valid = ((edge1+edge2)>edge3) && ((edge2+edge3)>edge1) && ((edge1+edge3)>edge2);
if(Valid) {
sum=edge1+edge2+edge3;
cout<<"The perimeter is "<<sum<<endl;
}else
cout<<"valid is not available."<<endl;
return 0;
}
using namespace std;
int main() {
int edge1, edge2, edge3;
int sum=0;
cout<<"Enter three edges : "; cin>>edge1>>edge2>>edge3;
bool Valid = ((edge1+edge2)>edge3) && ((edge2+edge3)>edge1) && ((edge1+edge3)>edge2);
if(Valid) {
sum=edge1+edge2+edge3;
cout<<"The perimeter is "<<sum<<endl;
}else
cout<<"valid is not available."<<endl;
return 0;
}
ifelse()문_세수 크기 비교
#include <iostream>
using namespace std;
int main() {
int x,y,z;
int min, mid, max;
cout<<"세 수를 입력하세요. : "; cin>>x>>y>>z;
if((x>y) && (x>z)) {
max=x;
if(y>z){
mid=y; min=z;
}else {
mid=z; min=y;
}
} else if((y>x) && (y>z)) {
max=y;
if(x>z){
mid=x; min=z;
}else{
mid=z; min=x;
}
} else {
max=z;
if(x>y){
mid=x; min=y;
}else{
mid=y; min=x;
}
}
cout<<"최대값 : "<<max<<"\n중간값 : "<<mid<<"\n최소값 : "<<min<<endl;
return 0;
}
using namespace std;
int main() {
int x,y,z;
int min, mid, max;
cout<<"세 수를 입력하세요. : "; cin>>x>>y>>z;
if((x>y) && (x>z)) {
max=x;
if(y>z){
mid=y; min=z;
}else {
mid=z; min=y;
}
} else if((y>x) && (y>z)) {
max=y;
if(x>z){
mid=x; min=z;
}else{
mid=z; min=x;
}
} else {
max=z;
if(x>y){
mid=x; min=y;
}else{
mid=y; min=x;
}
}
cout<<"최대값 : "<<max<<"\n중간값 : "<<mid<<"\n최소값 : "<<min<<endl;
return 0;
}
2017년 4월 13일 목요일
switch()함수이용_요일계산
#include <iostream>
using namespace std;
int main() {
int today;
int addDay;
cout<<"일요일 0 월요일 1 화요일 2 수요일 3\n목요일 4 금요일 5 토요일 6"<<endl;
cout<<"오늘의 요일 입력 : "; cin>>today;
cout<<"오늘로부터 며칠 후의 요일을 출력할 것인지 입력 : "; cin>>addDay;
int futureDay=(today+addDay)%7;
cout<<"오늘은 ";
switch(today){
case 0 : cout<<"일요일"; break;
case 1 : cout<<"월요일"; break;
case 2 : cout<<"화요일"; break;
case 3 : cout<<"수요일"; break;
case 4 : cout<<"목요일"; break;
case 5 : cout<<"금요일"; break;
case 6 : cout<<"토요일"; break;
}
cout<<" 이고 미래의 날은 ";
switch(futureDay){
case 0 : cout<<"일요일"; break;
case 1 : cout<<"월요일"; break;
case 2 : cout<<"화요일"; break;
case 3 : cout<<"수요일"; break;
case 4 : cout<<"목요일"; break;
case 5 : cout<<"금요일"; break;
case 6 : cout<<"토요일"; break;
}
cout<<" 이다."<<endl;
return 0;
}
using namespace std;
int main() {
int today;
int addDay;
cout<<"일요일 0 월요일 1 화요일 2 수요일 3\n목요일 4 금요일 5 토요일 6"<<endl;
cout<<"오늘의 요일 입력 : "; cin>>today;
cout<<"오늘로부터 며칠 후의 요일을 출력할 것인지 입력 : "; cin>>addDay;
int futureDay=(today+addDay)%7;
cout<<"오늘은 ";
switch(today){
case 0 : cout<<"일요일"; break;
case 1 : cout<<"월요일"; break;
case 2 : cout<<"화요일"; break;
case 3 : cout<<"수요일"; break;
case 4 : cout<<"목요일"; break;
case 5 : cout<<"금요일"; break;
case 6 : cout<<"토요일"; break;
}
cout<<" 이고 미래의 날은 ";
switch(futureDay){
case 0 : cout<<"일요일"; break;
case 1 : cout<<"월요일"; break;
case 2 : cout<<"화요일"; break;
case 3 : cout<<"수요일"; break;
case 4 : cout<<"목요일"; break;
case 5 : cout<<"금요일"; break;
case 6 : cout<<"토요일"; break;
}
cout<<" 이다."<<endl;
return 0;
}
소문자대문자변환(dowhile문)
#include <iostream>
using namespace std;
int main() {
char small;
char capital;
do {
cout<<"소문자를 입력하세요. : "; cin>>small;
if(small=='Q') break;
capital=small-32;
cout<<"변환된 대문자는"<<capital<<" 입니다."<<endl;
continue;
}while(small>='a' && small<='z');
return 0;
}
using namespace std;
int main() {
char small;
char capital;
do {
cout<<"소문자를 입력하세요. : "; cin>>small;
if(small=='Q') break;
capital=small-32;
cout<<"변환된 대문자는"<<capital<<" 입니다."<<endl;
continue;
}while(small>='a' && small<='z');
return 0;
}
setw()함수와 중첩for문 연습
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int i, j;
//i칸을 띄우려면 setw(i+자기자리)
cout<<setw(27)<<"Multiplication Table"<<endl;
cout<<setw(4)<<"|";
for(i=1;i<10;i++)
cout<<setw(3)<<i;
cout<<"\n--------------------------------"<<endl;
for(i=1;i<10;i++) {
cout<<i<<setw(3)<<"|";
for(j=1;j<10;j++) {
cout<<setw(3)<<i*j;
if(j==9)
cout<<"\n";
}
}
return 0;
}

#include <iomanip>
using namespace std;
int main() {
int i, j;
//i칸을 띄우려면 setw(i+자기자리)
cout<<setw(27)<<"Multiplication Table"<<endl;
cout<<setw(4)<<"|";
for(i=1;i<10;i++)
cout<<setw(3)<<i;
cout<<"\n--------------------------------"<<endl;
for(i=1;i<10;i++) {
cout<<i<<setw(3)<<"|";
for(j=1;j<10;j++) {
cout<<setw(3)<<i*j;
if(j==9)
cout<<"\n";
}
}
return 0;
}

피드 구독하기:
글 (Atom)






