Check Armstrong number or Not in R Programming Language

Delve into the world of Armstrong numbers in R programming, a fascinating mathematical concept. An Armstrong number (also known as a narcissistic or pluperfect digital invariant) is a number that is the sum of its own digits each raised to the power of the number of digits. Learn the algorithm to check whether a given number is an Armstrong number or not, and grasp the essence of this intriguing mathematical property in R programming.

An Armstrong number, also known as narcissistic number, is a number that is equal to the sum of the cubes of its own digits.
For example, 370 is an Armstrong number since 370 = 3*3*3 + 7*7*7 + 0*0*0.


num = as.integer(readline(prompt="Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while(temp > 0) {
digit = temp %% 10
sum = sum + (digit ^ 3)
temp = floor(temp / 10)
# display the result
if(num == sum) {
print(paste(num, "is an Armstrong number"))
} else {
print(paste(num, "is not an Armstrong number"))

Output 1:

Enter a number: 153
[1] "153 is an Armstrong number"

Output 2:

Enter a number: 212
[1] "212 is not an Armstrong number"
Scroll to Top