Armstrong Number in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Armstrong number in C | Here we will develop the C program to check number is Armstrong number or not?

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 .

C program to check Armstrong number of order Three

Check Armstrong number in C using while loop

 #include<stdio.h>
 int main()
 {
      int number, temp, rem, sum=0;

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

      temp=number;
      while( number!=0 )
      {
          rem = number%10;
          sum += (rem*rem*rem);
          number /= 10; //number=number/10
      }

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

      return 0;
 }

Output:-

Enter number: 150
150 is not an Armstrong number of order 3.

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

Note for Code:: Blocks IDE users:-

Many of you may be using CodeBlocks for developing C programs. There is a small problem with codeblocks IDE. The above code works fine in other IDE or environments, but in the codeblocks IDE it shows “153 is not an Armstrong number”.

You can verify it by debugging the code, or paste the below while loop in place of the original loop in the program, it will show the execution flow.

while( number != 0 )
{
remainder = number%10;
printf("Remainder=%d\t",remainder);
int power = pow(remainder, n);
printf("power=%d\t",power);
sum += power; //sum=sum+pow(remainder,n)
printf("sum= %d\n",sum);
number /= 10; //number=number/10
}

Output:-
Enter number: 153
Remainder=3 power=27 sum= 27
Remainder=5 power=124 sum= 151
Remainder=1 power=1 sum= 152
153 is not an Armstrong number of order 3.

Where the problem comes:- For 5^3 it gives result 124

It happens only for the number 153. For other numbers, it works fine.

Conclusion:- If you are using Code:: Blocks IDE then instead of using the pre-defined pow() function of math.h header file, write your own user-defined function to calculate the power of the number.


C program to Check Armstrong Number of order N

 #include<stdio.h>
 #include<math.h>
 int main()
 {
      int number, temp, n, remainder, sum=0;

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

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

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

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

      return 0;
 }

Output:-

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

Enter number: 153
Enter order: 5
153 is not an Armstrong number of order 5.

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 it “undefined reference to sqrt (or other mathematical functions)” even include math.h header

Check Armstrong number of order N in a given range

 #include<stdio.h>
 #include<math.h>
 int main()
 {
      int min, max, order;
      int temp, remainder, sum;

      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++)
      {
        temp = i;
        sum = 0;

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

        if(i == sum) printf("%d\t", i);
      }

      return 0;
 }

Output:-

Enter min & max value of the range: 1 1000
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!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Reply