I saw that lxmxn was playing with a batch script to find prime numbers, so I also played one, but this one is much easier. There is no fault tolerance check for input, and it is not guaranteed that the result will not have an overflow error. It is just a boring thing for leisure:
[ Last edited by namejm on 2007-1-24 at 02:59 PM ]
@echo off
setlocal enabledelayedexpansion
:begin
cls
set flag=
set num1=
set num2=
set /p num1= Please enter the first number:
set /p num2= Please enter the second number:
if %num1% leq %num2% (set min=%num1%) else set min=%num2%
:: This if statement can be changed to if %num1% gtr %num2% set /a num1=%num2%,num2=%num1%,
:: to reduce the number of variables, then the next %min% in the for statement has to be modified accordingly to %num1%
for /l %%i in (%min%,-1,2) do (
set GCD=%%i
set /a mod1=%num1%%%%%i
set /a mod2=%num2%%%%%i
if !mod1! equ 0 if !mod2! equ 0 set flag=1&goto end
)
:end
if defined flag (
set /a LCM=%num1%/%GCD%*%num2%
echo The greatest common divisor of %num1% and %num2% is %GCD%, and the least common multiple is !LCM!
) else (
set /a LCM=%num1%*%num2%
echo The greatest common divisor of %num1% and %num2% is 1, and the least common multiple is !LCM!
)
pause
goto begin
[ Last edited by namejm on 2007-1-24 at 02:59 PM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
考虑问题复杂化,解决问题简洁化。

