R Program to find the sum of even and odd digits of a number


In this program, You will learn how to find the sum of even and odd digits of a number in R.


Even sum is : = 2 + 4 = 6

Odd sum is : = 3 + 5 = 8

Example: How to find the sum of even and odd digits of a number in R

{
    n = as.integer(readline(prompt = "Enter a number :"))
    s = 0
    sod = 0

    while (n > 0) {
      r = n %% 10
      if (r %% 2 == 0) {
        s = s + r
      } else{
        sod = sod + r
      }
      n = n %/% 10
    }
    print(paste("Sum of the even digits :", s))
    print(paste("Sum of the odd digits :", sod))
}

Output:

Enter a number :2345
[1] "Sum of the even digits : 6"
[1] "Sum of the odd digits : 8"