Armstrong Number in C

What is Armstrong Number?

An Armstrong Number in C is an n-digit base b number, where the sum of its (base b) digits raised to the power n equals the number itself. Examples of Armstrong numbers include 0, 1, 153, 370, 371, 407, etc.

Armstrong Number Formula:

wxyz = pow(w,n) + pow(x,n) + pow(y,n) + pow(z,n)

Example:

Let’s look at 407 as an example to understand why it’s an Armstrong number.

407 = 4*4*4 + 0*0*0 + 7*7*7 
    = 64 + 0 + 343 
    = 407
Problem Description

Write a C program to check if a given number is Armstrong. If the number is an Armstrong then display it is an Armstrong number else display it is not an Armstrong number.

advertisement
advertisement
Problem Solution

1. Take a number as input. For example, n.
2. Check if the sum of all digits raised to power n equals the given number or not.
3. If it is, then it is an Armstrong number. Otherwise, it is not an Armstrong number.
4. Exit.

To understand this problem better, let’s solve the Armstrong number problem using the following methods:

Method 1: (3-digit Armstrong Number)

In this approach, we will only check whether a 3-digit number entered by the user is an Armstrong number or not.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Examples
  • If the entered number is 371, then 371 = 3*3*3 + 7*7*7 + 1*1*1 = 27 + 343 + 1 = 371. Since the sum of the cubes of every digit is equal to 371, the output will be “The given number is an Armstrong number.”
  • If the entered number is 150, then 150 = 1*1*1 + 5*5*5 + 0*0*0 = 1 + 125 + 0 = 126. Since the sum of the cubes of every digit is not equal to 150, the output will be “The given number is not an Armstrong number”
Program/Source Code

Here is the source code of the C Program to check whether a given 3-digit number is Armstrong or not. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Check whether a given Number is Armstrong
 */
 
#include <stdio.h>
#include <math.h> // header file for pow() function
 
void main()
{
    int number, sum = 0, rem = 0, cube = 0, temp;
 
    printf ("Enter a number:");
    scanf("%d", &number);
    temp = number;
    while (number != 0)  // loop will continue until the number is not 0.
    {
        rem = number % 10;  // This will calculate the last digit of the number.
        cube = pow(rem, 3); // The cube of the last digit will be calculated here.
        sum = sum + cube;
        number = number / 10; // Now our least significant bit of the number be removed.
    }
    if (sum == temp)
        printf ("The given number is an Armstrong number");
    else
        printf ("The given number is not an Armstrong number");
}
Program Explanation

1. In this C program, we are reading the integer value using ‘number’ variable.
2. An Armstrong number is an n-digit base b number, such that the sum of its digits raised to the power n is the number itself. Hence, 371 because 33 + 73 + 13 = 27 + 343 + 1 = 371.
3. Using while loop checks the value of ‘number’ variable is not equal to 0.
4. If the condition is true, execute the iteration of the loop.
5. The ‘rem’ variable is used to compute the modulus of the value of ‘number’ variable by 10 and ‘cube’ variable is used to compute the cube of the value of ‘rem’ variable using pow().
6. Then ‘sum’ variable is used to compute the summation of the value of ‘sum’ variable with the value of ‘cube’ variable.
7. The If-else condition statement is used to check both the value of ‘sum’ variable and the value of ‘temp’ variable are equal.
8. If the condition is true, then it will print Armstrong number. Otherwise, it will execute the else condition statement and print not Armstrong number.

Time Complexity: O(log(n))
The above program for checking whether a number is Armstrong or not has a time complexity of O(log(n)) as while loop runs for log(n) times because at each iteration the number is divided by 10, where n is the number given as input.

Space Complexity: O(1)
In armstrong number program, the space complexity is O(1) as no extra variable is taken to store the value. All of the initialized variables take constant O(1) space.

advertisement
Runtime Test Cases

Testcase 1: In this case, we enter the number “371” as input to check whether it is a armstrong number or not.

Enter a number: 371
The given number is an Armstrong number

Testcase 2: In this case, we enter the number “150” as input to check whether it is a armstrong number or not.

Enter a number: 150
The given number is not an Armstrong number

advertisement
Method 2: (N-digit Armstrong Number)

In this approach, we will be checking whether any N-digit number entered by the user is an Armstrong number or not.

Examples
  • If the entered number is 1634, then 1634 = 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634. Since the sum of the cubes of every digit is equal to 1634, the output will be “The given number is an Armstrong number.”
  • If the entered number is 1600, then 1600 = 14 + 64 + 04 + 04 = 1 + 1296 + 0 + 0 = 1297. Since the sum of the cubes of every digit is not equal to 1600, the output will be “The given number is not an Armstrong number”
Program/Source Code:

Here is the source code of the C Program to check whether a given number is Armstrong or not. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Check whether a given Number is Armstrong
 */
 
#include <stdio.h>
#include <math.h>
 
void main()
{
    int number, sum = 0, rem = 0, cube = 0, temp, n=0;
 
    printf ("enter a number\n");
    scanf("%d", &number);
    temp = number;
    int num = number;
    while(num!=0)   // time complexity-->O(log n)
    {
        num = num/10;                                   
        n++;      // count of digits n
    }
    while (number != 0)	  // time complexity-->O(log n)
    {
        rem = number % 10;
        cube = pow(rem, n);
        sum = sum + cube;
        number = number / 10;
    }
    if (sum == temp)
        printf ("The given number is an Armstrong number");
    else
        printf ("The given number is not an Armstrong number");
}
Program Explanation

In our earlier program, we were only finding if a 3-digit number is Armstrong or not whereas in this approach we can check if a number is Armstrong or not irrespective of the count of digits in that given number.

The approach of this method is to calculate the power of each digit but instead of passing ‘3’ in the power section we need to pass the count of digits ‘n’.

The first while loop is calculating count of digits in the number and the second while loop is for finding the sum of the power of each digit and for checking whether the number is Armstrong or not.

Time Complexity: O(log(n))
O(log(n)) + O(log(n)) = O(log(n))
The above program for checking whether a number is Armstrong or not has a time complexity of O(log(n)) as both the while loop runs for log(n) times because at each iteration the number is divided by 10, where n is the number given as input.

Space Complexity: O(1)
In the above program, space complexity is O(1) as no extra variable is taken to store the value. All the variables initialized takes constant O(1) space.

Runtime Test Cases

Testcase 1: In this case, we enter the number “1634” as input to check whether it is a armstrong number or not.

Enter a number: 1634
The given number is an Armstrong number

Testcase 2: In this case, we enter the number “1600” as input to check whether it is a armstrong number or not.

Enter a number: 1600
The given number is not an Armstrong number

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.