본문 바로가기
Programming/C# * Unity

[C#] for문으로 최소공배수 구하기

by 고막고막 2019. 3. 6.

Solution 1

int max = 0;

Console.WriteLine("첫 번째 숫자를 입력하세요");
string str1 = Console.ReadLine();
int num1 = Int32.Parse(str1);

Console.WriteLine("두 번째 숫자를 입력하세요");
string str2 = Console.ReadLine();
int num2 = Int32.Parse(str2);

if (num1 < num2)
    max = num2;
else
    max = num1;
for(;true; max++)
{
    if (max%num1 == 0 || max % num2 == 0)
        break;
}
    Console.WriteLine(max);
}

Solution 2

int num0 = 0, num1 = 0;
Console.WriteLine("첫 번째 수 입력");
num0 = Int32.Parse(Console.ReadLine());
Console.WriteLine("두 번째 수 입력");
num1 = Int32.Parse(Console.ReadLine());

for(int i=1; ; i++)
{
    if (i % num0 == 0 && i % num1 == 0)
        Console.WriteLine(i);
            break;
}

'Programming > C# * Unity' 카테고리의 다른 글

[Unity] 05. 충돌(Colliders)  (0) 2019.03.07
[Unity] 04. 애니메이션(Animation)  (0) 2019.03.07
[C#] while문과 do while문 비교  (3) 2019.03.05
[Unity] 03. 지형(Terrain)  (0) 2019.03.04
[C#] switch~case문 응용  (0) 2019.03.04