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;
namespace Syntax14
{
class Program
{
static void Main(string[] args)
{
int smallNum;
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
|
![](https://blog.kakaocdn.net/dn/tiBYz/btqyywLW2Ys/BloYlbVPc7JTgiIhjSaQ9K/img.png)
'Console Programming > C# Console' 카테고리의 다른 글
6일차 문자열 표현식들 (0) | 2019.09.25 |
---|---|
성적입력 프로그램 2일차 (0) | 2019.09.24 |
5일차 입력받은 수의 약수 구하기 (0) | 2019.09.24 |
5일차 1~20까지 숫자중 3의배수 이고 6의배수인 값의 합을 출력 (0) | 2019.09.24 |
5일차 year를 입력받아 윤년인지 아닌지 판별하기 (0) | 2019.09.24 |