File tree Expand file tree Collapse file tree 1 file changed +22
-15
lines changed
Expand file tree Collapse file tree 1 file changed +22
-15
lines changed Original file line number Diff line number Diff line change 1- program fizz_buzz
2- integer :: i
1+ program fizzbuzz
2+ implicit none
3+ integer , parameter :: n = 100
4+ integer :: i
35
4- do i = 1 ,100
5- if ((modulo (i,3 ) == 0 ) .and. (modulo (i,5 ) == 0 )) then
6- write (* ,' (a)' ) " FizzBuzz"
7- else if (modulo (i,3 ) == 0 ) then
8- write (* ,' (a)' ) " Fizz"
9- else if (modulo (i,5 ) == 0 ) then
10- write (* ,' (a)' ) " Buzz"
11- else
12- write (* ,' (I0)' ) i
13- end if
14- end do
15-
16- end program fizz_buzz
6+ do i = 1 , n
7+ write (* ,' (A)' ) fizzbuzz_string(i)
8+ end do
9+
10+ contains
11+
12+ pure function fizzbuzz_string (x ) result(str)
13+ integer , intent (in ) :: x
14+ character (len= 8 ) :: str
15+
16+ str = ' '
17+
18+ if (mod (x,3 ) == 0 ) str = ' Fizz'
19+ if (mod (x,5 ) == 0 ) str = trim (str)// ' Buzz'
20+ if (len_trim (str) == 0 ) write (str,' (I0)' ) x
21+ end function fizzbuzz_string
22+
23+ end program fizzbuzz
You can’t perform that action at this time.
0 commit comments