Skip to content

Instantly share code, notes, and snippets.

@VisualAcademy
Last active October 28, 2021 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VisualAcademy/45eb2d3417975106182c88b940e06f13 to your computer and use it in GitHub Desktop.
Save VisualAcademy/45eb2d3417975106182c88b940e06f13 to your computer and use it in GitHub Desktop.
C 언어 반복문과 연산자 함께 사용하기
//[?] 반복문과 연산자 함께 사용하기
#include <stdio.h>
int main(void)
{
//[1] 1부터 5까지 3개씩 출력하는 프로그램
for (int i = 1; i <= 5; i++)
{
printf("%d\t", i);
if (i % 3 == 0)
{
printf("\n");
}
}
printf("\n");
//[2] 1~100까지 정수의 합을 구하는 프로그램
int sum = 0; // 합을 저장할 변수
for (int i = 1; i <= 100; i++)
{
sum += i; // 누적
}
printf("1부터 100까지의 합: %d\n", sum);
//[3] 1~100까지 정수 중 짝수의 합을 구하는 프로그램
int even = 0;
for (int i = 1; i <= 100; ++i)
{
if (i % 2 == 0)
{
even += i; // 짝수만...
}
}
printf("1부터 100까지의 짝수의 합: %d\n", even);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment