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

  • Thread starter Thread starter teng125
  • Start date Start date
AI Thread Summary
The discussion focuses on developing a recursive algorithm to calculate the checksum of an integer n, defined as the sum of its digits. Participants clarify that the algorithm should sum the individual digits of n, not the numbers from 1 to n. The use of the div and mod functions is emphasized, with examples provided to illustrate how to extract digits from the number. The checksum for the example n = 198 is confirmed to be 18, derived from the sum of its digits (1 + 9 + 8). Overall, the conversation aims to guide the development of the algorithm while stressing the importance of understanding the problem's requirements.
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
 

Similar threads

Replies
5
Views
3K
Replies
2
Views
3K
Replies
5
Views
6K
Replies
16
Views
2K
2
Replies
86
Views
22K
2
Replies
60
Views
11K
2
Replies
52
Views
12K
2
Replies
67
Views
14K
Back
Top