5일차 최대공약수 구하기

Console Programming/C# Console 2019. 9. 24. 17:36

두 수를 입력받아 최대공약수 구하기

 

 

두 수 중 작은 수에서부터 1까지 for문을 반복하고, 

만약 input1 % i ==0 && input2 % i ==0 이면

i의 값을 출력하고 for문을 종료한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax14
{
    class Program
    {        
        static void Main(string[] args)
        {
            int smallNum;
          
            int input1 = Int32.Parse(Console.ReadLine());
            int input2 = Int32.Parse(Console.ReadLine());
          
            if (input1 < input2)
            {
                smallNum = input1;
            }
            else
            {
                smallNum = input2;
            }
           for (int i = smallNum; i>=1; i--)
            {
                if(input1 % i ==0 && input2 % i ==0)
                {                    
                    Console.WriteLine(i);
                    break;
                }
            }
          
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

: