Posted By

chema_ar on 10/22/14


Tagged

loops diamond f90


Versions (?)

Diamond


 / Published in: Fortran
 

This is a piece of code to print out a diamond with loops. It should be improved and protected.

  1. program diamond
  2. integer :: n = 7 ;
  3. integer :: mid = 0;
  4. integer :: lmin = 0;
  5. integer :: rmax= 0;
  6. integer :: i,j;
  7. if(mod(n,2)/=0) then
  8. i = 0;
  9. mid = n/2;
  10. do while (i<n)
  11. if (i<=mid) then
  12. lmin = mid-i;
  13. rmax = mid+i;
  14. else
  15. lmin = mid - (n-i)+1;
  16. rmax = mid + (n-i)-1;
  17. end if
  18. j = 0;
  19. do while (j<n)
  20. if (j>=lmin .AND. j<=rmax) then
  21. write(*,"(A)",advance="no") " *"
  22. else
  23. write(*,"(A)",advance="no") " ."
  24. end if
  25. j = j + 1;
  26. end do
  27. print*,""
  28. i = i +1;
  29. end do
  30.  
  31. end if
  32. read *,
  33.  
  34. end program diamond

Report this snippet  

You need to login to post a comment.