Armstrong numbers program using C

Armstrong number:  Before going to the C program to work with Armstrong numbers we should know what an Armstrong number is. So, let’s try to understand about what is an Armstrong number.

A number which contains n digit like a1a2a3a4a5….an will be an Armstrong number if the number a1a2a3a4a5….an = a1n + a2n + a3n + a4n + a5n + …. + ann

For example, 0, 1, 2, 3, 153, 371, 407, 1634, 8208 etc are some of the Armstrong numbers. Now lets see why these numbers are called Armstrong numbers.

In case of 371 we can check that why it is an Armstrong number. In this number total digits are 3.

Now,

371 = 33 + 73 + 13

= 27 + 343 + 1

= 371

Again for the number 1634 we get here the total digits = 4.

Now,

1634 = 14 + 64 + 34 + 44

= 1 + 1296 + 81 + 256

= 1634

Check an integer is Armstrong number or not

// C program to check Armstrong number
#include <stdio.h>

int armFunction(int r, int d){   // function to check Armstrong numbers
  int a, b = 1;

  for (a = 1; a <= d; a++){
    b = b * r;
   }
  return b;
}

int main(){
  int num, i, rem, sum = 0, digit = 0;

  printf("Input an integer to check it : ");
  scanf("%d", &num);

  i = num;
  while(i != 0){     // Counting number of digits
    digit++;
    i = i/10;
  }

  i = num;
  while(i != 0){
    rem = i % 10;
    sum = sum + armFunction(rem, digit);
    i = i/10;
  }

  if (num == sum){
    printf("\n%d is an Armstrong number.\n", num);
  }else{
    printf("\n%d is not an Armstrong number.\n", num);
  }
  return 0;
}

Output of the Armstrong number program:

armstrong number program in c
c program to check an integer is armstrong number or not, armstrong numbers

Printing Armstrong numbers

In this printing Armstrong numbers program the user will give two input of integer. Then the program will print all the Armstrong numbers between the two integers. So, let’s see the C program bellow to serve this purpose.

// C program to print Armstrong number
#include <stdio.h>

int armCheck(int);
int checkPower(int, int);

int main(){
  int x, y, i;

  printf("Enter lower limit : ");
  scanf("%d", &x);
  printf("Enter maximum limit : ");
  scanf("%d", &y);
  printf("\nArmstrong numbers between %d and %d are : \n", x, y);

  for (i = x; i <= y; i++){
    if (armCheck(i) == 1)
      printf("%d\n", i);
    }
  return 0;
}

int armCheck(int num){
  long long p, sum = 0;
  int rem, digit = 0;

  p = num;

  while (p != 0){
    digit++;
    p = p/10;
  }

  p = num;

  while (p != 0){
    rem = p%10;
    sum = sum + checkPower(rem, digit);
    p = p/10;
  }

  if (num == sum){
    return 1;
  }else{
    return 0;
  }
}

int checkPower(int n, int r){
  int j, k = 1;

  for (j = 1; j <= r; j++){
    k = k * n;
    }
  return k;
}

Output of this C program

Enter lower limit : 50
Enter maximum limit : 1000

Armstrong numbers between 50 and 1000 are :
153
370
371
407
Enter lower limit : 1
Enter maximum limit : 2000

Armstrong numbers between 1 and 2000 are :
1
2
3
4
5
6
7
8
9
153
370
371
407
1634

Previous pageAlphabet triangle

Next pagePalindrome numbers

More C programming and projects

C learning topics