C program to check Armstrong Number

Armstrong Number : An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.

For example :

\(153\;=\;1^3\;+\;5^3\;+3^3\;=1+125+27\;=153 \)
\(1634\;=\;1^4\;+\;6^4\;+3^4\;+4^4\;=\;1+1296+81+256\;=\;1634\)
Armstrong Number

C Program for Armstrong Number of Three Digits :

/**********************************

 https://bptutorials.com

*************************************/
#include <stdio.h>

void main()
{
    int num, original_num, sum = 0, remndr;
    // Ask User to input number
    
    printf("Welcome to https://bptutorials.com: \n");
    printf("Enter 3 digit number : ");
    // store inputed no. in variable num
    scanf("%d", &num);

    // Preserve user input in variable original_num
    original_num = num;
    
    while(num !=0)
    {
        remndr = num % 10 ;
        // Cubes of every digit stored in sum variable
        sum = sum + (remndr * remndr * remndr);
        num = num / 10 ;
    }
        
        /* If sum of cubes of every digit is equal to the number entered by 
        user then it is Armstrong number*/
        
        if(original_num == sum)
        printf("%d is an Armstrong Number", original_num);
        else
        printf("%d is not an Armstrong Number", original_num);
        
      
    }

Output :

Armstrong number in C
C program to check Armstrong Number

C Program to Check Armstrong Number of n digits :

/*********************************************

https://bptutorials.com
C program to check Armstrong number of n digits

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


int main ()
{
  int num, originalNum, remndr, sum = 0, count = 0;
  printf ("Welcome to https://bptutorials.com \n");
  printf ("Enter a number :  ");
  scanf ("%d", &num);
  originalNum = num;

  while (originalNum != 0)
    {
      originalNum = originalNum / 10;
      ++count;
    }
  originalNum = num;

  while (originalNum != 0)
    {
      remndr = originalNum % 10;
      sum = sum + pow (remndr, count);
      originalNum = originalNum / 10;
    }

  if (sum == num)

    printf ("%d is an Armstrong Number", num);
  else
    printf ("%d is not an Armstrong Number", num);

  return 0;
}

Output :

c program to check Armstrong number of n digits
Armstrong number in c of n digits
C program to check Armstrong Number 1
Share This!

Leave a Reply

Your email address will not be published. Required fields are marked *