Recursive Algorithm for CHKSUM Calculation: div and mod Functions | n >= 0

  • Thread starter Thread starter teng125
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 2K views
teng125
Messages
416
Reaction score
0
pre n >= 0
post checksum(n) = CHKSUM(n)
reads n
changes -
mem -

Remark: Let n be an integer number. Furthermore, CHKSUM(n) = “sum of digits of n”. checksum is the name of the module to be developed.

Develop a recursive algorithm for the above specification. Assume and use functions
div function and mod function.

can smby pls show me how to write this algorithm

thanx
 
Physics news on Phys.org
Well first off, you need to know exactly what your algorithm is supposed to do. Looks like your CHKSUM(n) is supposed to find the sum of the digits of n. So, take an example. If you have n = 198...then checksum should return what value?

After that it should be pretty straightforward, if you know what value it's supposed to be.
 
does it means sum up all the values from 1 to 198 if n=198??
actually i don'tknow what does the algorithm above means at all

why does it needs to use functions div function and mod function.

if possible,pls explain to me

thanx
 
No it does not mean that...it means to sum up the "digits" that make up the number 198. So the number 198 is made up of 3 "digits"...which numbers and what is their sum?
 
pls show me how to write this recursive algorithm.I need it very much,pls

thanx
 
We're not going to do your problem for you.

The checksum of 198 should be 1 + 9 + 8 = 18.
Here is another hint: 198 mod 10 = 8.
(198 - 198 mod 10) / 10 = 19