Find Armstrong Number C Program

C Program To Find Armstrong Number

Here’s a C Program To Find Armstrong Numbers. Learn How To Check if a Number is an Armstrong Number or not in C Programming Language. It is important that we should know How A For Loop Works before getting further with the C Program Code.


What is an Armstrong Number?

If the Sum of Cubes of Individual Digits of an Integer is equivalent to the Integer, then it can be regarded as an Armstrong Number. Armstrong Number is similar to a Narcissistic Number.

Example

153 = (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3)
371 = (3 * 3 * 3) + (7 * 7 * 7) + (1 * 1 * 1)

We have written 3 different variants to Check Armstrong Integers in C Programming Language which includes While Loop, For Loop and Functions.

How does this program work?

  1. The original number is copied into a temporary variable
  2. The last digit of the original number is extracted and cubed and added in the sum variable.
  3. The last digit is extracted till the original number is not equal to zero.
  4. After extraction and cubing, the sum variable is compared with the temporary variable.
  5. If sum == temp, Number is an Armstrong Number, else it is not an Armstrong Integer.

Method 1: Find Armstrong Number in C Programming using While Loop

Method 2: C Program To Check Armstrong Number using For Loop


Method 3: Find Armstrong Number in C Programming using Function

Output

C Program To Find Armstrong Number using While and For Loops

In case you get any Compilation Errors or any doubts in this C Program To Find Armstrong Number, let us know about it in the Comment Section below.

Recommended Programs
C Program To Check if a Number is Magic Number or Not
C Program To Add Two Numbers without Arithmetic Operator
C Program To Find Perfect Numbers
C Program To Check Narcissistic Number
C Program To Check Palindrome Numbers
C Program To Print Hello World Without Semi Colon
Code To Remove Vowels in a String in C Programming
Find Smallest Digit in a Number C Program
C Program To Check Leap Year
100+ C Programs For Programming Interviews

Tushar Soni

I am Tushar Soni, Co - Founder of CodingAlpha. I am a computer science student from India and passionate about Web Development and Programming. Connect with me on Facebook | LinkedIn | Google Plus

28 thoughts on “Find Armstrong Number C Program

  • November 29, 2015 at 4:53 pm
    Permalink

    Thanks! It works perfect!

    Reply
  • May 28, 2016 at 11:22 am
    Permalink

    Wow. Thanks for so many armstrong number programs in different ways. This has made my work more easier.

    Reply
  • July 29, 2016 at 12:33 am
    Permalink

    I am getting confused in void main and int main. The armstrong number programs that are here jave int main() declarations. Is it compulsory to use them?

    Reply
    • July 29, 2016 at 12:48 am
      Permalink

      Hi Pankaj! int main() is primarily used when you want to fetch arguments using Command Line. Otherwise, void main() will work. This Armstrong Number C Programs above can be made to work through command line by adding two arguments in the main function. (int argc, int *argv).

      Reply
  • July 29, 2016 at 12:46 am
    Permalink

    In this Armstrong Integer Program, I am getting confused with the modulus operator. Can you help me understand the use of Modulus Operator?

    Reply
    • July 31, 2016 at 1:01 pm
      Permalink

      This is just awesome. So many ways to write armstrong number program in c! It helped me to clear my concepts functions and also how to convert for loop into while loop.

      Reply
  • August 20, 2016 at 2:44 pm
    Permalink

    So many methods have made my basics clear about Armstrong Numbers and C programming looping system. I have a doubt. When we enter three numbers, the program shows correct output, but if the number consists of more than 4 digits, it shows a wrong output.

    Reply
    • August 20, 2016 at 2:47 pm
      Permalink

      Hi Piyu. Armstrong Number program calculates only upto 3 digits. If you want to find armstrong number more than 3 digits, you should check its big brother Narcissistic Number. The Narcissistic Numbet C Program link is given in the above program.

      Reply
  • August 21, 2016 at 12:45 am
    Permalink

    I tried using float num instead of taking an integer data type. It is showing error. Please help.

    Reply
    • August 21, 2016 at 12:46 am
      Permalink

      Hi Rajendra. Well, the float variables cannot be used an an operand with Modulus Operator. That is why you would be getting errors. Please check which datatypes work with Modulus Operator and then select a suitable datatype.

      Reply
  • August 29, 2016 at 2:35 pm
    Permalink

    You have executed this Armstrong Number C Program in Linux. Is it compatible if I execute it on Windows platform?

    Reply
    • September 10, 2016 at 3:08 am
      Permalink

      Yes. This Armstrong Integer code is written in Linux without any IDE. There should not be any problem to run this code on Windows platform.

      Reply
  • September 8, 2016 at 3:28 pm
    Permalink

    Why not use Recursion to calculate Armstrong Numbers,

    Reply
    • September 10, 2016 at 3:08 am
      Permalink

      Yes. You may use Recursive function also to find solution to Armstrong Number problem.

      Reply
  • September 10, 2016 at 3:05 am
    Permalink

    I think the condition num!=0 can also be changed to num>0 or num>=1 in this code.

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

    Reply
    • September 10, 2016 at 3:06 am
      Permalink

      Yes. Your code will work perfectly fine in finding armstrong integers.

      Reply
  • September 13, 2016 at 10:09 pm
    Permalink

    Why do we need to copy the armstrong number variable num1 into temp? I also see that the sum variable os compared to temp. Why can’t we compare it to num1 variable?

    Reply
  • October 3, 2016 at 10:06 pm
    Permalink

    How do I cube the remainder variable? Is there a built in function for it or do we have to make a user defined function?
    for(temp = num1; num1 != 0;)
    {
    rem = num1%10;
    sum = sum + cube(rem);
    num1 = num1/10;
    }

    Reply
  • November 5, 2016 at 1:58 pm
    Permalink

    The narcissistic number code is much better than just an Armstrong number code. The armstrong number code can find the cubes only upto 3 digits and what after that? On the other hand, the narcissistic number is an advanced version of the armstrong number which can calculate cubes of numbers having more than 3 digits.

    Reply
  • November 12, 2016 at 8:40 am
    Permalink

    Can we get possible number of Armstrong numbers within a range? Ex,if input is 10 ,10 Armstrong numbers should be output?

    Reply
    • November 21, 2016 at 7:37 am
      Permalink

      Yes. You may have to add a For loop and run it till the ending limit of your range.

      Reply
  • December 13, 2016 at 12:41 pm
    Permalink

    Gladly work your program

    Reply
  • April 19, 2017 at 7:18 pm
    Permalink

    Brother which compiler and text editor do you use please tell me.

    Reply
  • April 20, 2017 at 10:21 am
    Permalink

    Hi Rahul. It is GCC Compiler(Linux Ubuntu’s default C Compiler) with the gEdit editor (Linux Ubuntu’s default text editor).

    Reply

Join The Discussion