5. 단순 반복문
1. for문 a -> b 1씩 증가
for( 초기화식 ; 조건식 ; 증감식 ) {
반복할 코드
}
1) 차례로 출력
#include <iostream>
using namespace std;
int main() {
for (int i = 5; i <= 17; i++)
cout << i << ' ';
return 0;
}
2) 입력받는 수 부터 100까지 출력
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = n; i <= 100; i++)
cout << i << ' ';
return 0;
}
3) 출력결과 1
https://www.codetree.ai/missions/4/problems/reading-k201517?&utm_source=clipboard&utm_medium=text
4) 출력결과 8
https://www.codetree.ai/missions/4/problems/reading-k201528?&utm_source=clipboard&utm_medium=text
5) 출력결과 10
https://www.codetree.ai/missions/4/problems/reading-k201530?&utm_source=clipboard&utm_medium=text
6) 1부터 n까지 출력
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cout << i << ' ';
return 0;
}
2. for문 a -> b 2씩 증가
1) 증가시키며 출력하기
#include <iostream>
using namespace std;
int main() {
for (int i = 5; i <= 17; i += 2)
cout << i << ' ';
return 0;
}
2) 정수 입력받아 배수 출력
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= 5; i++)
cout << n * i << ' ';
return 0;
}
3) 홀수만 출력
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
for (int i = a; i <= b; i++)
{
if (i % 2)
cout << i << ' ';
}
return 0;
}
3. for문 b -> a 1씩 감소
1) 감소시키며 출력하기
#include <iostream>
using namespace std;
int main() {
for (int i = 17; i >= 5; i--)
cout << i << ' ';
return 0;
}
2) 특정숫자 도달하기 2
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = n; i >= 1; i--)
cout << i << ' ';
return 0;
}
3) b부터 a까지 감소
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
for (int i = b; i >= a; i--)
cout << i << ' ';
return 0;
}
4) 홀수만 출력 2
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> b >> a;
for (int i = b; i >= a; i--)
{
if (i % 2)
cout << i << ' ';
}
return 0;
}
4. while문 a -> b 1씩 증가
while (조건) {
여기에 조건을 만족할 때만 수행되었으면
하는 코드를 작성
}
1) 증가시키며 출력하기 2
#include <iostream>
using namespace std;
int main() {
int i = 10;
while (i <= 26)
cout << i++ << ' ';
return 0;
}
2) n까지 숫자 출력
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int i = 1;
while (i <= n)
cout << i++ << ' ';
return 0;
}
3) 별 n개 출력하기
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int i = 0;
while (i++ < n)
cout << '*' << endl;
return 0;
}
5. while문 a -> b 2씩 증가
1) 증가시키며 출력하기 3
#include <iostream>
using namespace std;
int main() {
int i = 10;
while (i <= 26)
{
cout << i << ' ';
i += 2;
}
return 0;
}
2) 3의 배수 출력하기
#include <iostream>
using namespace std;
int main() {
int n;
int a = 3;
cin >> n;
while (a <= n)
{
cout << a << ' ';
a += 3;
}
return 0;
}
3) 짝수만 출력
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int i = a;
while (i <= b)
{
cout << i << ' ';
i += 2;
}
return 0;
}
6. while문 b -> a 1씩 감소
1) 감소시키며 출력하기 2
#include <iostream>
using namespace std;
int main() {
int i = 26;
while (i >= 10)
{
cout << i << ' ';
i--;
}
return 0;
}
2) n부터 1까지 숫자 출력
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int i = n;
while (i >= 1)
cout << i-- << ' ';
return 0;
}
3) 짝수만 출력 2
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> b >> a;
int i = b;
while (i >= a)
{
cout << i << ' ';
i -= 2;
}
return 0;
}
7. n번 반복하기
1) 문자 출력하기
#include <iostream>
using namespace std;
int main() {
char chr;
cin >> chr;
for (int i = 0; i < 8; i++)
cout << chr;
return 0;
}
2) 문자열 출력하기
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cout << "LeebrosCode" << endl;
return 0;
}
3) a/b 출력
https://www.codetree.ai/missions/4/problems/a-divide-b?&utm_source=clipboard&utm_medium=text
4) n번 더하기
#include <iostream>
using namespace std;
int main() {
int a, n;
cin >> a >> n;
for (int i = 0; i < n; i++)
{
a += n;
cout << a << endl;
}
return 0;
}
8. if 안의 for
1) 숫자의 증감
#include <iostream>
using namespace std;
int main() {
char c;
int n;
cin >> c >> n;
if (c == 'A')
{
for (int i = 1; i <= n; i++)
cout << i << ' ';
}
if (c == 'D')
{
for (int i = n; i >= 1; i--)
cout << i << ' ';
}
return 0;
}
2) 높은 수에서 낮은 수까지
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
if (a > b)
{
for (int i = a; i >= b; i--)
cout << i << ' ';
}
else
{
for (int i = b; i >= a; i--)
cout << i << ' ';
}
return 0;
}
3) 자연수면 출력
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
if (a > 0)
{
for (int i = 0; i < b; i++)
cout << a;
}
else
cout << 0;
return 0;
}
'코드트리' 카테고리의 다른 글
[코드트리 조별과제] C++ 기초 : 단순 반복문 (2) (0) | 2024.08.19 |
---|---|
[코드트리 조별과제] C++ 기초 : 조건문 (0) | 2024.08.06 |
[코드트리 조별과제] C++ 기초 : 연산자 (0) | 2024.07.29 |
[코드트리 조별과제] C++ 기초 : 입출력 (0) | 2024.07.22 |
[코드트리 조별과제] C++ 기초 : 출력 (0) | 2024.07.15 |