R Program to check Armstrong number


February 15, 2022, Learn eTutorial
1086

What is an Armstrong number?

An Armstrong number is a magical number where the sum of each digit raised to the power of count of digits will give the number itself. It is also called a narcissistic number or a plus-perfect 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.

We have a detailed description of the concepts of Armstrong Numbers in our blog The basics of Armstrong numbers. It won't disappoint you, will surely help you to understand these numbers clearly.

Commonly we look for an Armstrong number program to check a 3 digit number is Armstrong or not, ie., If the sum of cubes of digits is the same as the number or not. So we have 3 digits Amstrong number program in our R program collection, you can refer to it also.

Here we are checking an Armstrong number without any digit constraints.

How to check the given number is Armstrong or not

A number can call an Armstrong number if the sum of each digit raised to the power of n equals that number, where the number has n digits in it. We need to calculate the sum of the power n of each digit.

  • For a 2 digit number, it will be the sum of squares of each digit,
  • For a 3 digit number, it will be the sum of cubes of each digit,
  • For a 4 digit number, it will be the sum of the 4th power of each digit and so on.

For example, take 371 the digits of 371 are 3,7 & 1. The cubes of each number are 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 into num by providing an appropriate message to the user using 'prompt' inside the readLine() function. Find how many digits are there in the number and save it to n_digit because we need to calculate the powers according to this value only. Also, save the integer value of the number read into temp.

Now using a while loop extract each digit and find the sum of the power raised to count of digits(n_digit) of each digit. After calculation, check if 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: Set n_digit with number of digits of the number entered

STEP 3: Initialize sum=0 and save the value of num to temp

STEP 4: 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 power raised to n_digit 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 = readline(prompt="Enter a number: ")  # read user input
n_digit <- nchar(num)     # nchar() gives count of characters of string num
# initialize sum,temp
sum = 0
temp = as.integer(num)
# find the sum of the cube of each digit
while(temp > 0) {
    digit = temp %% 10
    sum = sum + (digit ^ n_digit)
    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: " 
9474
"9474 is an Armstrong number"