Final state after a condition-controlled digit loop

The Fortran program below repeats its loop body while a condition holds rather than for a fixed count. The intrinsic function mod(n, 10) returns the remainder when n is divided by 10, and n / 10 is integer division because both operands are integers.


fortran
program digits
  implicit none
  integer :: n, s, c
  n = 4071
  s = 0
  c = 0
  do while (n > 0)
     s = s + mod(n, 10)
     n = n / 10
     c = c + 1
  end do
  print *, s, c, n
end program digits


Give the values of s, c and n that the print statement displays, each as a whole number.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Fortran and Other Imperative Languages Questions: