This is the program I am compiling in Fortran. It calculates two different formulas for even and odd numbers and it stops when you get 1 as result. If I choose N = 50 for example, I get all the values displayed in the same line. I would like maximum 40 characters (including numbers and commas) printed per line. I also need to store the last numbers (4,2,1), but I don't know how.
That's what I am getting when I compile the program.
50, 25, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1,
That's what I want (40 characters per line):
50, 25, 76, 38, 19, 58, 29, 88, 44, 22,
11, 34, 17, 52, 26, 13, 40, 20, 10, 5,
16, 8, 4, 2, 1,
program numbers
implicit none
integer(kind=4) :: i, n
print*,'Enter the N value'
read*, n
do while (n.ne.1)
if (mod(n,2) .eq.0) then
n = n/2
write(6,'(i0,a)',advance='no') n,','
else if (mod(n,2) .ne.0) then
n = (3*n)+1
write(6,'(i0,a)',advance='no') n,','
else
cycle
end if
end do
end program numbers