In this program, You will learn how to find the sum of digits of a number in R.
Example: How to find the sum of digits of a number in R
1 2 3 4 5 6 7 8 9 10 11 12 13 | { n = as.integer(readline(prompt = "Enter a number :")) s = 0 while (n > 0) { r = n %% 10 s = s + r n = n %/% 10 } print(paste("Sum of the digits is :", s)) } |
Output:
1 2 3 4 | Enter a number :213 [1] "Sum of the digits is : 6" |