Check Armstrong number or Not in R Programming Language

Check Armstrong number or Not in R Programming Language

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.

Program:
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”

No Comments

Post a Reply