Armstrong Number C Program

By | September 17, 2015

Let us understand what is an ArmstrongInteger Series and let us learn how we can implement Armstrong Number in C programming language.

What is an Armstrong Number Series?

An Armstrong number is the sum of cubes of the individual digits of the number. If the sum of the digits is equal to the number, then it is said to be an Armstrong Integer.

An Armstrong integer is similar to a narcissistic number. However, there’s a difference between an Armstrong number and narcissistic number.

An Armstrong integer is essentially a 3 digit number whereas a narcissistic number is a 4 or more than 4 digit number. It is also usually regarded as 4 digit Armstrong Number.

Armstrong Number Formula

Armstrong Number Formula in Mathematical Expression for Calculation Armstrong in C Programming

Example

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

Armstrong numbers are essentially 3 digit integers. However, if you intend to calculate 4 digit armstrong number in C programming, you need to check the narcissistic number c program.

Algorithm For Armstrong Number in C Programming

Armstrong Series Logic Explanation

  1. The input value is copied into a temporary variable.
  2. The last digit of the input value is extracted, cubed and added to the sum variable.
  3. The last digit is extracted till the input value is not equal to zero.
  4. After the process is completed and the remainder is zero, the sum variable is compared with the temporary variable.
  5. If the sum and temporary variables are equivalent, the input value is an Armstrong Number, else it is not an Armstrong Integer.

Note: This C program for Armstrong Numbers is compiled with GNU GCC compiler on Linux Ubuntu 14.04 operating system. However, these codes are compatible with all other operating systems.

 

Must Read: C Program To Check Palindrome Numbers

Method 1: C Program To Find Armstrong Numbers using While Loop

 

Must Read: Tribonacci Series C Program

Method 2: Generate Armstrong Numbers in C Programming using For Loop

Must Read: C Program To Check if a Number is Magic Number or Not

Method 3: C Program To Check Armstrong Number using Function

Must Read: C Program To Check Strong Numbers

Output

Learn How To Check Armstrong Number in C Programming using Function, While Loop and For Loop with Output, Explanation and Example.

Let’s discuss more on Armstrong number in C programming in the comment section below if you have any compilation errors and any doubts about the same.

For more information on Armstrong series, check NCTM.

32 thoughts on “Armstrong Number C Program

  1. Pankaj Dhende

    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
    1. Tushar Soni Post author

      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
  2. Vishal Kadam

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

    Reply
    1. Dipak Kale

      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
  3. Rajendra Prasad

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

    Reply
    1. Tushar Soni Post author

      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
  4. Rab Khan

    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
  5. Aarav Rathod

    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
  6. Radhika Khanna

    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
  7. Chandu

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

    Reply
    1. Damodar Das

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

      Reply
  8. Tushar Soni Post author

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

    Reply
  9. Vamshi NCH

    So, as far as I understood is that Armstrong number is a 3 digit number and the 4 digit armstrong number is a narcissistic number. Please correct me if I am wrong!

    Reply
  10. Gaurav Saraswat

    Hi tushar
    Can u plz explain why the sum in the if statement is not equalized to num1 in method 2 using for loop?Rather it is equalized to temp. Plz explain

    Reply
    1. Tushar Soni Post author

      When you perform: num1 = num1/10, the original num1 gets modified. So, you cannot compare the sum variable with the modified value. Hence, we are using temp variable to check whether the original num1 is equal to sum or not.

      Reply

Let's Discuss