17+ C Armstrong Checker & Finder Examples

This post contains a total of 17+ Hand-Picked C Armstrong Number Checker & Finder Program Examples with Source Code. All the Armstrong Number Checker & Finder programs are made using C Programming Language.

You can use the source code of these programs for educational purpose with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Nova

Made by Nova. Program To Check Whether The Entered Number Is Armstrong Number Or Not. ( Source )

      *** Armstrong Number ***

Enter Any Number :  153
Result : 153 is an Armstrong Number.
#include <stdio.h>
#include <math.h>
#include <stdbool.h>

bool checkArmstrong(unsigned int);
int getDigitCount(unsigned int);

int main() {
    unsigned int num;
    bool flag;
    printf("%32s","*** Armstrong Number ***\n\n");
    printf("Enter Any Number :  ");
    scanf("%u",&num);
    printf("%u\nResult : ",num);
    flag = checkArmstrong(num);
    flag ? printf("%u is an Armstrong Number.",num) : printf("%u isn't an Armstrong Number.",num);
    return 0;
}

bool checkArmstrong(unsigned int num) {
    int count = getDigitCount(num);
    unsigned int temp = num;
    unsigned int sum = 0;
    while(temp!=0) {
        int rem = temp%10;
        sum += pow(rem, count);
        temp/=10;
    }
    return sum==num ? true : false;
}

int getDigitCount(unsigned int num) {
    int cnt = 0;
    while(num!=0) {
        cnt++;
        num/=10;
    }
    return cnt;
}

2. By BroFar

Made by BroFar. C Program to find the Armstrong numbers between a given range. ( Source )

Armstrong numbers between 0 to 10000 are: 
0	
1	
2	
3	
4	
5	
6	
7	
8	
9	
153	
370	
371	
407	
1634	
8208	
9474	

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

      int main() { 
          int n, bfo, b, o, f; 
          int y = 0; int z = 10000;
          char  user[] = "BrofarOps"; 
          char date[] = "08/22/2019";
          printf("Created by %s on %s \n\n",user,date);
          printf("The sequence of base 10 narcissistic numbers starts: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474... also known as Armstrong numbers\n\n");
          printf("Armstrong numbers between %d to %d are: \n\n",y,z); 
             for(f=y; f<=z; f++) { o = 0; 
             n = f; 
             b = (int) log10(n) + 1; 
             /* Calculating the sum of power of digits */ 
             while(n > 0) {
              bfo = n % 10; 
             /* Use ceil() function to overcome any rounding errors by pow() */
              o = o + ceil(pow(bfo, b)); 
              n = n / 10; } 
         /* Is it an Armstrong number */ 
            if(f == o) { 
             printf("%20d\t\n", f);
              } 
        } printf("\nThank you for running my code please upvote");
        
   return 0; }
   

3. By Abhi Varshini

Made by Abhi Varshini. ( Source )

Enter an integer: 234 is not an Armstrong number.
#include <stdio.h>
#include <math.h> 
int main() 
{ 
    int number, originalNumber, remainder, result = 0, n = 0 ;
    printf("Enter an integer: ");
    scanf("%d", &number);
    originalNumber = number;
    while (originalNumber != 0)
    {
        originalNumber /= 10;
        ++n;
    }
    originalNumber = number;
    while (originalNumber != 0)
    {
        remainder = originalNumber%10;
        result += floor(pow(remainder, n));                    originalNumber /= 10; 
    } 
        if(result == number)
        printf("%d is an Armstrong number.", number);
        else 
        printf("%d is not an Armstrong number.", number);
        return 0;
     }
/*Explanation:-
If the user input is 370,then it can be evaluated as 3 power 3+7 power 3+0 power 3.
The power value is taken as 3 because there are 3 numbers in the given digit.
Then, prints 370 is an Armstrong number.*/

4. By ASIF BILAKHIYA

Made by ASIF BILAKHIYA. C Program to check if a number is Armstrong or not, and also to print all the Armstrong numbers in that range. ( Source )

----------------------
Number:370
----------------------

----------------------
370 is Armstrong
----------------------

----------------------
ArmStrong from 0-370
----------------------
0
1
153
370
----------------------
#include <stdio.h>
void developer_asif_bilakhiya()
{
    printf("\n__________________________________________");
printf("\n|\t|\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\t\t\t\t\n|\t| ->Developer:Asif Bilakhiya\t\t\t\t\t\t\t\t\t|\n|\t| ->https://www.sololearn.com/Profile/928735/?ref=app\t\t\t\t\t|\n|\t|\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t| if(u_satisfy(true))\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t| {\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t| like(please);\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t| }\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\n");
 printf("------------------------------------------");

}
int cube(int rem)
{
return (rem*rem*rem);
}
int is_fn(int t1)
{
int t=t1;
int rem=0;
int sum=0;
while(t!=0)
{
    rem=t%10;
    sum=sum+cube(rem);
    t=t/10;    
}

if(sum==t1)
{
    return 1;  
}
else
{
   return 0;    
}
    return 0;
}
int main() {
int x=0;
scanf("%d",&x);
printf("\n----------------------\n");
printf("Number:%d",x);
printf("\n----------------------\n");
int t=x;
printf("\n----------------------\n");
if(is_fn(t)==1)
{
    printf("%d is Armstrong",x);
}
else
{
      printf("%d is Not Armstrong",x); 
}
printf("\n----------------------\n");

printf("\n----------------------\n");
printf("ArmStrong from 0-%d",x);
printf("\n----------------------\n");
t=0;
while(t!=x+1)
{
if(is_fn(t)==1)
{
    printf("%d\n",t);
}
//printf("%d",t);
   t=t+1; 
}
printf("----------------------\n");
developer_asif_bilakhiya();
    return 0;   
}

5. By Thanigai Velan

Made by Thanigai Velan. Simple program to check Armstrong number. ( Source )

Enter the number : 533
No,it is not an armstrong number..
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n;
    printf ("Enter the number : ");
    scanf("%d",&n);
    int temp,ans=0,x;
    temp=n;
    while(temp>0)
    {
        x=temp%10;
        temp=temp/10;
        ans=ans+(x*x*x);
    }
    if(ans==n)
    {
        printf ("It is an armstrong number..");
    }
    else 
    {
        printf ("No,it is not an armstrong number..");
    }
    return 0;
}

6. By Divesh Singh

Made by Divesh Singh. This program finds whether the supplied number is an Armstrong number or not. ( Source )

Enter a number: 371
371 is an Armstrong number
#include <stdio.h> 
#include <math.h>

int digitCount(unsigned long num){
    /* Utility function to count number of digits*/
    int count=0;
    while(num!=0){
        num/=10;
        ++count;
    }
    return count;
}

int armstrong(unsigned long num){
    unsigned long temp, remainder, result=0;
    int digit=digitCount(num);
    temp=num;
    while (num >0) { 
        remainder = num%10; 
        result += pow(remainder,digit);
        num /= 10; 
        } 
    if(result == temp) 
        return 1;
    else
        return 0;
}

int main() { 
    unsigned long number;
    printf("Enter a number: "); 
    /* Enter between the range 0 to 4294967294*/
    scanf("%lu", &number); 
    printf("%lu\n", number);
   
    if(armstrong(number)||number==153)
    /* Had to hardcore the 2nd condition due to some errors with the SL compiler*/
        printf("%lu is an Armstrong number\n",number);
    else
        printf("%lu is not an Armstrong number\n",number);
    return 0; 
}

7. By Sharon Shelton

Made by V. ( Source )

Enter the number to be checked:322 is not Armstrong
#include <stdio.h>

int main() {
    int n,r,sum=0,a;
    printf("Enter the number to be checked:");
    scanf("%d",&n);
    a=n;
    while(n>0)
    {
        r=n%10;
        sum=sum+(r*r*r);
        n=n/10;
    }
    if(a==sum)
        printf("%d is Armstrong",a);
    else
    printf("%d is not Armstrong",a);
	return 0;
}

8. By Shruti Bansal

Made by Shruti Bansal. ( Source )

enter a number3 322
not an armstrong no.
#include<stdio.h>
#include<math.h>
int main()
{

    int input,x=0,p=1,a,b,c,num=0;
    printf("enter a number");
    scanf("%d",&input);
    a=input;
    while(a!=0)
    {
               a=a/10;
               ++num;
               }
               printf("%d\n",num);
     b=input;
     while(b!=0)
     {
                c=b%10;
                p=pow(c,num);
                x=x+p;
                b=b/10;
                }
      if(x==input)
      printf("it is an armstrong no.");
      else
      printf("not an armstrong no.");
               
     
     return 0;
     }

9. By G Murali

Made by G Murali. ( Source )

enter a number: 371
the given number is armstrong number
#include <stdio.h>

int main() 
{
  int a,n,b=0,t;
  printf("enter a number:\n");
  scanf("%d",&n);
  t=n;
  while(n>0)
  {
   a=n%10;
   b=b+a*a*a;
   n=n/10;
  }
  if(b==t)
  {
      printf("the given number is armstrong number");
  }
  else
  {
    printf("the given number is not armstrong number");
  }
return 0;
}

10. By Indira

Made by Indira. Program to check whether a number is armstrong or not. ( Source )

Enter a number:311
Number is not Armstrong
#include <stdio.h>

int main(){
    int n1,dig,cub,org,arm=0;
    
    printf("Enter a number:");
    scanf("%d",&n1);
    printf("%d",n1);
    org=n1;
    while(n1>0){
        dig=n1%10;
        cub=dig*dig*dig;
        arm=arm+cub;
        n1=n1/10;
    }
    
    if(org==arm){
        printf("\nNumber is Armstrong.");
    }
    else{
        printf("\nNumber is not Armstrong");
    }
    return 0;
}

11. By Darshan Raval

Made by Darshan Raval. Program to get the armstrong numbers between a given range. ( Source )

---------------------------
ArmStrong Numbers : 
153
370
371
407
---------------------------
Program By Darshan Raval


#include <stdio.h>
#include <math.h>
void main() {

    // [ Variable Declaration ]
    int str,end,i,j,tmp,sum=0;
    
    // [ Take Input From User ] 
    scanf ("%d%d",&str,&end);
    
    // [ Logic Build & Print Output]
    
    printf ("---------------------------\n");
    printf ("ArmStrong Numbers : \n");
    for (i=str; i<=end; i++){
        tmp = i;
        sum = 0; 
        do{
            j = tmp % 10;
            sum = sum + j*j*j;
            tmp = tmp / 10;
        }while (tmp != 0);
        if (i == sum){
            printf ("%d\n",i);
        }
    }
    printf ("---------------------------");
    
    printf ("\nProgram By Darshan Raval");
}

12. By Kavita Bharti

Made by Kavita Bharti. ( Source )

enter the no 
244
not armstrong
#include <stdio.h>

int main()
 {
 int num,rem,cube,arm=0,number;
 printf("enter the no \n");
 scanf("%d",&num);
 number=num;
 while(number!=0)
 {
 rem=number%10;
 number=number/10;
 cube=rem*rem*rem;
 arm=arm+cube;
 }
 if(arm==num)
 printf("%d is armstrong",arm);
 else
 printf("not armstrong");

    return 0;
}

13. By Gen2oo

Made by V. ( Source )

Enter a number: 
Number 153 is an Armstrong Number.
//_______________________________________________________________________________
//
// This is a re-implementation of Armstrong Number Calculator I created in C++, 
// but this time in C. I use C++ most of the time, but in C there is not built-in 
// libraries for stack, vector, map, etc.
//
// I wanted a stack that uses object oriented class functions like in C++.
//     Stack implementation is hidden below because i want to show how it 
// can be used. if you wanna know how stack is implemented scroll to bottom.
//
//_______________________________________________________________________________
//
// An Armstrong number is when each digit of the number raised to the power of the
// total digit count, equals the number itself.
//
// Ex of Armstrong number is all numbers from 0-9 = (0^1) (1^1) (2^2)... etc.
//       Valid armstrong   = 371: (3 ^ 3) + (7 ^ 3) + (1 ^ 1) = (27 + 343 + 1) = 371 
//       Invalid armstrong = 42:  (4 ^ 2) + (2 ^ 2) = (16 + 4) = 20 
//_______________________________________________________________________________
//
#include <stdio.h>
#include <stdlib.h>

#define class struct
#define EXIT_ON_ALLOC_FAIL(p)                                       \
        do {                                                        \
            if (!p)                                                 \
                fputs("[Error]: Failed to create stack!\n"          \
                      "\t Not enough memory!\n", stderr), exit(0);  \
        } while (0)
#define bitsize_of(type) (sizeof(type) << 3)

typedef class mini_stack mini_stack;
static mini_stack *stack_create(int cap);
static long math_pow(int n, int p);

//
// variables are public by default, but could be wrapped in opaque struct 
// to make them encapsulated like in C++.
//
class mini_stack {
	int (*push) (mini_stack *s, int value);
	int (*pop)  (mini_stack *s);
	int (*top)  (const mini_stack *s);
	int (*empty)(const mini_stack *s);
	void (*show)(const mini_stack *s);
	void (*free)(mini_stack *s);
	int *data, cap, top_; 
};


int main(void)
{
    // can hold all digits of an int
	mini_stack *digits = stack_create(bitsize_of(int)); 
	int count, org, sum = 0, num = 0;
	
	// if allocation fails, kill the program :(
	EXIT_ON_ALLOC_FAIL(digits);        

	puts("Enter a number: ");
	scanf("%d", &num);
	org = num;

    // count all digits and add them to stack.
	for (count = 0; num; count++, num /= 10) 
	    digits->push(digits, num % 10);
	    

	while (!digits->empty(digits))
	    sum += math_pow(digits->pop(digits), count);
	
	printf("Number %d is %s Armstrong Number.\n", 
	        org, sum != org ? "not an" : "an");
	        
	digits->free(digits);

	return 0;
}





// Simple power of function so we dont have to include math.h
static long math_pow(int n, int p)
{
    long power;
    for (power = 1; p-- > 0; power *= n);
    return power;
}

//-------------------------------------------------------------------------
// This is where the stack is implemented.
//-------------------------------------------------------------------------
static int stack_push(mini_stack *stack, int value)
{
	if (stack->top_ >= stack->cap) {
		fputs("Stack overflow!\n", stderr);
		return 0;
	}
	stack->data[stack->top_++] = value;
	return 1;
}

// remove element from stack
static int stack_pop(mini_stack *stack)
{
	if (stack->top_ < 0) {
		fputs("Stack underflow!\n", stderr);
		return 0;
	}
	return stack->data[--stack->top_];
}

// return the top element
static int stack_top(const mini_stack *stack)
{
	return stack->data[stack->top_ > 0 ? stack->top_-1 : 0];
}

// check if stack is empty
static int stack_empty(const mini_stack *stack)
{
	return stack->top_ <= 0;
}

// Show all values in stack without removing.
static void stack_show(const mini_stack *stack)
{
	int i;
	for (i = 0; i < stack->top_; i++)
		printf("%d\n", stack->data[i]);
}

// free the stack because it's allocated on heap
static void stack_free(mini_stack *stack)
{
	free(stack->data);
	free(stack);
}

//
// This is the Stack Constructor. inside we create a dummy stack which 
// is pre-initialized with basic stack data. We do a shallow copy on it
// so our stack internal functions are initialized.
//    this stack wont resize itself so the cap must be the maximum size.
// if cap < 4, it is set to 4 just so the default size is not too small.
//
static mini_stack *stack_create(int cap)
{
    unsigned int allowed_cap = cap >= 4 ? cap : 4;
	mini_stack *new_stack = NULL, dummy_stack = {
		.data  = malloc(allowed_cap * sizeof(dummy_stack.data)),
		.cap   = allowed_cap,
		.top_  = 0,
		.push  = stack_push, 
		.pop   = stack_pop, 
		.top   = stack_top, 
		.empty = stack_empty, 
		.show  = stack_show, 
		.free  = stack_free
	};

	if (!dummy_stack.data) // Allocation failure
	    return NULL;
	new_stack = malloc(sizeof(*new_stack));
    if (new_stack)
          *new_stack = dummy_stack;
	return new_stack;
}

14. By Shaurya Barnwal

Made by Shaurya Barnwal. C program for find Armstrong Number. ( Source )

 Please enter no. to check for armstrong

Entered no.= 9
9 is a armstrong no.

	THANK YOU
//Example: 1-9,153,370,371,407,1634 

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

int main()
{
    int N,T,R,n=0,S=0;
    printf(" Please enter no. to check for armstrong\n");
    scanf("%d",&N);
    T=N;
    while(T!=0)
    {
        n+=1;
        T/=10;
    }
    T=N;
    for(T=N;T>0;T/=10)
    {
        R=T%10;
        S+=pow(R,n);
    }
    printf("\nEntered no.= %d",N);
    if(N==S)
    {
        printf("\n%d is a armstrong no.",S);
    }
    else
    {
        printf("\n%d is not armstrong no.",N);
        printf("\n\nPlease try again !");
     }
     printf("\n\n\tTHANK YOU");
    return 0;
}

15. By Sourav Dutta

Made by Sourav Dutta. ( Source )

You have entered 434

434 is not a Armstrong number
#include <stdio.h>

int main() {
    int n,r,sum=0,cube=1,flag;
    scanf("%d",&n);
    printf ("You have entered %d\n\n",n);
    flag=n;
    while(n>0){
        r=n%10;
        cube=r*r*r;
        sum=sum+cube;
        n=n/10;
    }
    if(sum == flag){
        printf ("%d is a Armstrong number",flag);
    }
    else{
        printf ("%d is not a Armstrong number",flag);
    }
    return 0;
}

16. By Andrew

Made by Andrew. You must specify the start and end values โ€‹โ€‹of the interval. The program will display all Armstrong numbers in this interval. ( Source )

First number of interval: 10
Last number of interval: 10000
153  370  371  407  1634  8208  9474  
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>

int stringLength(const char * string){
	
	char * cptr = string;
	while(* cptr)
	cptr++;
	return cptr - string;
}

bool ArmstrongNum(char arr[], int n) {
	
	int i, val, sumOfPow = 0;
	
	for (i = 0; i < n; i++) {
		val = arr[i] - '0';
		sumOfPow += pow(val, n);
	}
	if (sumOfPow == atoi(arr))
		return true;
	else
		return false;
}

int main() {
	bool ArmstrongNum(char arr[], int n);
	int stringLength(const char * string);
	int first, last, i, n;
	char s[80];
	
	printf("First number of interval: ");
	scanf("%i", &first);
	printf("%i", first);
	printf("\nLast number of interval: ");
	scanf("%i", &last);
	printf("%i\n", last);
	
	for (i = first; i <= last; i++) {
		sprintf (s, "%i", i);
		n = stringLength(s);
		if (ArmstrongNum (s, n))
			printf("%s  ", s);
	}
	
	return 0;
}

17. By Taki BMD

Made by Taki BMD. ( Source )

Give the number 123
123 is not an Armstrong number
#include <stdio.h>
#include <math.h>

int calcNumOfDig(int numb) {
    int nDig;
    
    while(numb != 0) {
        nDig ++;
        numb /= 10;
        }
    
    return nDig ;
}

// calcul of the sum of the digits in power of n
int sumD(int num, int n){
    int sum = 0;

    int copy , rem;
    
    copy = num;
    while (copy != 0) {
        rem = pow(copy%10,n);
        sum += rem;
        copy /= 10;
    }
    return sum;
}


int main() {
    int number , nod , summ;
    
    do {
        printf ("Give the number\n");
        scanf("%d",&number);
    } while (number <= 0);
    
    nod = calcNumOfDig (number);
    summ = sumD (number , nod);
    if(summ == number) 
    printf("%d is an Armstrong number\n", number);
    else printf("%d is not an Armstrong number\n",number);
    return 0;
}

18. By Hargun Singh Sahni

Made by Hargun Singh Sahni. To check whether given number is armstrong or not. ( Source )

Enter a number to check whether given number is armstrong or not
9
The entered number 9 is not armstrong 
#include<stdio.h>
int main()
{
  int a,n,sum=0,rem=0;
  printf("Enter a number to check whether given number is armstrong or not \n");
  scanf("%d", &a);
  n=a;
  while(n!=0)
  {
  	rem=n%10;
  	sum+=rem*rem*rem;
  	n/=10;
  }
  if(sum==a)
   printf("The entered number %d is armstrong \n", a);
  else
   printf("The entered number %d is not armstrong \n", a);
  return 0;
}