Program to check Armstrong number in C using function

armstrong-number-in-c-using-function-program

Armstrong Number in C using function Program Description:

Write a C Program to check the Armstrong number in C using function with the program algorithm. The program should accept an integer number from the user and display if it is an Armstrong number or not using a C function.

Example Input:

Enter a Number: 93084

Output:

93084 is an armstrong number

What is an N-Digit Armstrong Number?

A positive number of n digits is said to be an Armstrong number of order n, If the sum of the power of  n of each digit in the number is equal to the original number itself.

If the number is abcd is Armstrong number, Then it should satisfy the below equation.

abcd = pow(a, n) + pow(b, n) + pow(c, n) + pow(d, n)

Where n is the number of digits in the given number. here the number is abcd, So the value of n is 4.

Pre-Requisites:

We are going to use the functions to check the Armstrong number, So it is recommended to know the basics of the following topics.

Armstrong Number in C using function Program Algorithm:

  1. Take the input from the user and store it in a variable num.
  2. Check if the num is a negative number, If so display an error message to the user
  3. As we discussed earlier, We need to calculate power(eachDigit, nDigit).
  4. We need to Calculate the Number of digits in the number. Which is used to calculate the power. We used a function countDigits and passed the input number num to calculate the number of digits and countDigits function returns the number of digits in the number( num).
  5. Then we have defined a function named isArmstrong, The isArmstrong function takes an integer number ( num) as input and returns a boolean. If the given number is an Armstrong number, then isArmstrong function returns true, Otherwise, it returns false.
  6. To check the Armstrong number, we need to go through each digit of the given number num using the Loop.
    • At Each Iteration,
      • Get the digit from the number i.e numrem = temp %10;
      • Calculate the power of rem with nDigits(Number of digits in num). We are using the Power function from the math.h header file – pow(rem, nDigits); to calculate the power. We can also use Custom Power Function.
      • Update the arm variable. – arm = arm + <strong>pow</strong>(rem, nDigits);
      • Remove the processed digit using the temp/10 operation. – temp = temp/10;
  7. Once the above loop is terminated, check the value of arm number. If the arm variable is equal to num, Then given the number num is N-digit Armstrong Number.
    • If arm == num, return true
    • otherwise, return false
  8. In the main function, We are using the if statement to check the result of isArmstrong function and printing the result based on the return value.

Armstrong Number in C using function Program:

Here are the prototype details of the isArmstrong function

  • function_name: isArmstrong
  • argument_list: int
  • return_type: bool

As we are using the bool type, We need to include the stdbool.h header file. We are also using the pow function, So we need to include the math.h as well.

Program Output:

Compile and run the program, Don’t forget to include the -lm compilation option. ( as we are using the math.h file, we need to include the math library)

Compile the program

$ gcc armstrong-func.c -lm

The above command generates a.out executable file. Let’s run the executable using the ./a.out command.

Test Case 1: Positive Numbers ( Armstrong numbers and non-Armstrong numbers test)

As we can see from the above output, The number 407 is an Armstrong number. Let’s check a few more numbers

check-armstrong-number-in-c-using-function-program-output

Test Case 2: Pass the Negative Numbers:

As expected, The program is displaying the error on negative numbers. Here we are terminating the program if the user enters a negative number, We can use the do while loop or Goto Statement to ask the user input again.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] Armstrong Number using Function in C language […]

  2. […] Armstrong Number using Function in C language […]

  3. […] Armstrong Number using Function in C language […]

Leave a Reply