// 1~100까지 3의 배수 또는 4의 배수의 합을 while문을 사용해서 구하시오.
using System;

public class 배수의합
{
    public static void Main()
    {  
        //[1] Input
        int 합 = 0;
       
        //[2] Process
        #region  while문으로 작성해 본 코드
        int i = 1;      // 초기식
        while (i <= 100)     // 조건식
        {
            if (i % 3 == 0 || i % 4 == 0)   // 필터링(조건처리)
            {
                합 += i;    // 실행문
            }
            i++;    // 증감식
        }
        #endregion // #region~#endregion은 긴 코드를 접었다 펼수 있게 하는 기능.        
        //[3] Output
            Console.WriteLine(합);
    }

}

==============================================================================================

do ~ while문을 사용해서 구한 경우 ==> while문을 사용하는 경우와 결과값은 같다.

using System;

public class 배수의합
{
    public static void Main()
    {  
        //[1] Input
        int 합 = 0;
       
        //[2] Process
        #region do문으로 작성해 본 코드
        int i = 1;      //[1] 초기식부터
        do
        {
            if (i % 3 == 0 || i % 4 == 0)       //[!] 필터링 설정 후
            {
                합 += i;    //[4] 실행문을 실행
            }
            i++;    //[3] 증감식만큼
        } while (i <= 100);     //[2] 조건식까지
        #endregion

        //[3] Output
            Console.WriteLine(합);
    }
}

'.NET프로그래밍 > C# 3.5 SP1' 카테고리의 다른 글

C# 언어 사양  (0) 2009.08.05
C# 기본 문법 PPT  (0) 2009.08.05
13. 비트연산자  (0) 2009.08.04
12. 시프트연산자  (0) 2009.08.04
11. 짝수의 합 구하는 프로그램  (0) 2009.08.04
Posted by holland14
: