You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
521 B
R
25 lines
521 B
R
Check Armstrong number
|
|
# take input from the user
|
|
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: 23
|
|
[1] "23 is not an Armstrong number" |