fork download
  1. program euclidean_gcd
  2. implicit none
  3. integer :: a, b, temp
  4.  
  5. print *, '2つの自然数を入力してください(空白で区切る):'
  6. read *, a, b
  7.  
  8. ! 入力値が負でないか確認
  9. if (a <= 0 .or. b <= 0) then
  10. print *, '正の整数を2つ入力してください。'
  11. stop
  12. end if
  13.  
  14. ! ユークリッドの互除法
  15. do while (b /= 0)
  16. temp = b
  17. b = mod(a, b)
  18. a = temp
  19. end do
  20.  
  21. print *, '最大公約数は:', a
  22. end program euclidean_gcd
  23.  
  24.  
Success #stdin #stdout 0.01s 5320KB
stdin
3355 2379
stdout
 2つの自然数を入力してください(空白で区切る):
 最大公約数は:          61