R Program to check 3 digit armstrong number

What is an Armstrong number?

An Armstrong number is a number, where the sum of the powers (digit raised to the count of digits) of each digit of the number equals that number.

An integer number of n digits can be called an Armstrong number if it satisfies the following condition,

abcd...=an+bn+cn+dn+... 

where a,b,c,d... are the digits of the number and n is the count of digits of the number.

To understand more about the concepts on Armstrong Numbers, we assure you our blog The basics of Armstrong numbers will help you very much.

Here in this R program, we explained how to check a 3 digit number is Armstrong or not. We have a program to check a number with n digits is Armstrong or not in our R programs Section, we recommend you to go through that program also.

How to check the given 3 digit number is Armstrong or not

A three-digit number can call an Armstrong number if the sum of the cubes of each digit of the number equals that number. We need to calculate the sum of the cube of each digit.

For example take 371, the digits of 371 are 3,7 & 1. The cubes of each number is as shown below.

33 = 3 x 3 x 3 = 27

73 = 7 x 7 x 7 = 343

13 = 1 x 1 x 1 = 1

Then the sum of the cubes of the digit = 27+343+1

                                                             = 371  

How an Armstrong number check is implemented in R Program

Given below are the steps used in the R program to read value and check if it is an Armstrong number. In this R program, we accept the user's input, a 3 digit number into num by providing an appropriate message to the user using 'prompt' inside the readLine() function. Using a while loop extract each digit and find the sum of the cubes of each digit. After calculation check the sum is equal to the given number itself; if yes then it is an Amstrong number, otherwise, it is not an Amstrong number.

ALGORITHM

STEP 1: Prompting appropriate messages to the user take user input using readline() into variable num

STEP 2: Initialize sum=0 and move temp = num

STEP 3: Find the sum of the cube of each digit using while loop with condition temp > 0

  • Split a digit of number with a mod by 10 operation
  • Calculate the sum of cube of that digit with previous
  • Avoid that digit and take the rest of the digits by a division with 10 operation

STEP 4: Check the sum with num, if they are equal then print the number is an Armstrong number otherwise not.

R Source Code

                                          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)
}
if(num == sum) {
    print(paste(num, "is an Armstrong number"))
} else {
    print(paste(num, "is not an Armstrong number"))
}
                                      

OUTPUT

Enter a number: 230
[1] "230 is not an Armstrong number"

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