R Program to check a number is a perfect number or not


In this program, You will learn how to check a number is a perfect number or not in R.


Some list of perfect numbers is : 6, 28, 496, 8128

Example: How to check a number is a perfect number or not in R

{
    n <- as.integer(readline(prompt = "Enter a number :"))
    i = 1
    s = 0

    while (i < n) {
      if (n %% i == 0) {
        s = s + i
      }
      i = i + 1
    }

    if (s == n) {
      print(paste("The number is perfect :", n))
    } else{
      print(paste("The number is not perfect :", n))
    }
}

Output:

Enter a number :6
[1] "The number is perfect : 6"