Armstrong Number in C Using Function

In this post, we will write a program for Armstrong number in C using a function. Previously we had developed a C program to check whether the given number is an Armstrong number or not? Now we will do the same using a function in C.

A positive integer is called Armstrong number of order n if, abcd…. = an + bn + cn + dn + ….

For Example :- 153
13 + 53 + 33 = 1 + 125 + 27 = 153
So, 153 is an Armstrong number of order 3.

4150 = 45 + 15 + 55 + 05 = 1,024 + 1 + 3,125 + 0 = 4150
So, 4150 is an Armstrong number of order 5 .

A function is a block of code that performs a specific task. For example, the main is a function and every program execution starts from the main function in C programming. The function is a small program that is used to do a particular task. In C a big program divided into several small subroutines/functions/procedures. Hence C is a function-oriented programming language.

The C program is made of one or more pre-defined/user-defined functions. Every program must have at least one function with the name main. The execution of the program always starts from the main function and ends with the main function. The main function can call other functions to do some special task.

C Function to check Armstrong Number

Three is the default order and it means whenever we say X is an Armstrong number then actually X is an Armstrong number of Order three.

#include<stdio.h>

int isArmstrong(int number) 
{

  // declare variables
  int lastDigit = 0;
  int power = 0;
  int sum = 0;

  // temporary variable to store number
  int n = number;

  while(n!=0) {

     // find last digit
     lastDigit = n % 10;

     // find power of digit (order = 3)
     power = lastDigit*lastDigit*lastDigit;

     // add power value into sum
     sum += power;

     // remove last digit
     n /= 10;
  }

  if(sum == number) return 0;
  else return 1;
}

int main()
{
  int number;

  printf("Enter number: ");
  scanf("%d",&number);

  if(isArmstrong(number) == 0)
  printf("%d is an Armstrong number.\n", number);
  else
  printf("%d is not an Armstrong number.", number);

  return 0;
}

Output for different test-cases:-

Enter number: 153
153 is an Armstrong number.

Enter number: 4150
4150 is not an Armstrong number

Check Armstrong Number in C of order N Using Function

Procedure to check Armstrong number of order N

1) Take a variable and take an order to check
2) Declare variables lastDigitpower, and sum Initialize sum with 0
3) Take a temporary variable n to store numbers
4) Find the last digit of n
5) Calculate the power of that lastDigit with order i.e. pow(lastDigit, order)
6) Add the result into the sum
7) Remove the last digit
8) Repeat step 4 to 7 until the number becomes 0
9) Compare sum value and the actual number
==> If both are the same then it is Armstrong number of the given order
==> Else it is not Armstrong number of the given order

#include<stdio.h>
#include<math.h>

int checkArmstrong(int number, int order)
{

  // declare variables
  int lastDigit = 0;
  int power = 0;
  int sum = 0;

  // temporary variable to store number
  int n = number;

  while(n!=0) {

     // find last digit
     lastDigit = n % 10;

     // find power of digit
     power = pow(lastDigit, order);

     // add power value into sum
     sum += power;

     // remove last digit
     n /= 10;
  }

  if(sum == number) return 0;
  else return 1;
}

int main()
{
  int number, order;

  printf("Enter number: ");
  scanf("%d",&number);

  printf("Enter Order: ");
  scanf("%d",&order);

  if(checkArmstrong(number, order) == 0)
  printf("%d is an Armstrong number of order %d.\n", number, order);
  else
  printf("%d is not an Armstrong number of order %d", number,order);

  return 0;
}

Output for different test-cases:-

Enter number: 153
Enter Order: 3
153 is an Armstrong number of order 3.

Enter number: 4150
Enter Order: 5
4150 is an Armstrong number of order 5.

If you are using Linux/Unix OS and getting an error due to pow() function then read this “undefined reference to sqrt (or other mathematical functions)” even include math.h header

Check in a given range

C program to Check Armstrong number of order N in a given range.

#include<stdio.h>
#include<math.h>

int checkArmstrong(int number, int order)
{
  int temp, remainder, sum = 0;

  temp = number;

  while( temp != 0 )
  {
     remainder = temp%10;
     sum += pow(remainder, order);
     temp /= 10;
  }

  if(number == sum) return 0;

  else return 1;
}

int main()
{
  int min, max, order;

  printf("Enter min & max value of the range: ");
  scanf("%d %d",&min, &max);

  printf("Enter the order to check: ");
  scanf("%d", &order);

  printf("The Armstrong number of order %d are:\n", order);

  for(int i=min; i<=max; i++)
  if(checkArmstrong(i, order) == 0)
  printf("%d\t", i);

  return 0;
}

Output for different test-cases:-

Enter min & max value of the range: 1 10000
Enter the order to check: 3
The Armstrong number of order 3 are:
1 153 370 371 407

Enter min & max value of the range: 1 10000
Enter the order to check: 5
The Armstrong number of order 5 are:
1 4150 4151

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply