Armstrong Number in C

Armstrong Number in C

3 mins read2.5K Views 2 Comments
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Sep 6, 2024 16:09 IST

An Armstrong Number is defined as the sum of the nth power of each digit to an n-digit number that is equal to that number. Confused! Don’t worry. In this article, we will discuss everything about the Armstrong Number with the help of examples, and later in the article, we will also discuss how to check whether the given number is Armstrong Number or not using C programming.

2023_08_Armstrong-Number-in-C.jpg

In the previous articles, we have discussed:

This article will discuss different methods to check the Armstrong Number in C Programming.
But before that, let’s discuss what is an Armstrong Number.

Armstrong Number (or Narcissistic Number) is a special type of positive integer that is equal to the sum of its own digit raised to the power of the number of digits in that number. i.e.,
Any integer ‘abcd’ is known as an Armstrong number if,

abcd = a4 + b4 + c4 + d4

Example:

  • 153 = 13 + 53 + 33
  • 1634 = 14 + 64 + 34 + 44

Note: A number with n digits is called Armstrong Number of order n, where the sum of each digit powered by n is the same as the number.

a1a2a3….an = (a1)n + (a2)n + (a3)n + …. + (an)n

Until now, you have a better understanding of what is Armstrong’s number.

Armstrong Number in Java using Different Methods
Armstrong Number in Java using Different Methods
Learn to check if a number is an Armstrong number in Java using while loop or recursion. Understand the mathematical definition and see code examples for easy implementation
How to Find an Armstrong Number Using Python
How to Find an Armstrong Number Using Python
Learn how to find Armstrong numbers using Python with our step-by-step guide. Discover the algorithm to identify these special numbers, which are equal to the sum of their own digits...read more

Crack the code of C/C++ with our comprehensive guide; find details on top colleges, programmes and online courses.

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4.24 K
6 weeks
475
6 weeks
– / –
45 hours
4 K
80 hours
– / –
1 month
– / –
40 hours
– / –
2 months
– / –
1 year
4 K
80 hours

How to Find an Armstrong Number Using C Programming?

To check whether a given number is an Armstrong number or not, we will discuss two different methods:

Armstrong Number in C using Function


 
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
bool isArmstrong(int num) {
int originalNum = num;
int sum = 0;
int digitCount = 0;
// Count the number of digits in the input number
while (num != 0) {
num /= 10;
digitCount++;
}
// Reset num to the original value for further processing
num = originalNum;
// Calculate the sum of cubes of digits using mathematical formula
while (num != 0) {
int digit = num % 10;
sum += pow(digit, digitCount);
num /= 10;
}
// Check if the sum is equal to the original number
return (sum == originalNum);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isArmstrong(num)) {
printf("%d is an Armstrong number.\n", num);
} else {
printf("%d is not an Armstrong number.\n", num);
}
return 0;
}
Copy code

Output

2023_08_example-of-armstrong-number.jpg

Explanation

  • Define a function ‘isArmstrong’ that takes an integer as an argument.
  • Inside the function, count the number of digits in the input number.
  • Initialize a variable (named it as ‘sum’) to store the sum of the power (nth power) of the digits.
  • Calculate the ‘sum’ with the original input number using the mathematical formulas.
  • If the ‘sum’ is equal to the input number, return TRUE (TRUE indicate that the number is an Armstrong number); otherwise, return FALSE.

Armstrong Number in C using Loop


 
#include <stdio.h>
#include <math.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
int originalNum = num;
int sum = 0;
int digitCount = 0;
// Count the number of digits in the input number
while (num != 0) {
num /= 10;
digitCount++;
}
// Reset num to the original value for further processing
num = originalNum;
// Calculate the sum of cubes of digits using a loop
while (num != 0) {
int digit = num % 10;
sum += pow(digit, digitCount);
num /= 10;
}
// Check if the sum is equal to the original number
if (sum == originalNum) {
printf("%d is an Armstrong number.\n", originalNum);
} else {
printf("%d is not an Armstrong number.\n", originalNum);
}
return 0;
}
Copy code

Output

2023_08_example-2-of-armstrong-number.jpg

Explanation

  • Input the number whose Armstrong property needs to be checked. 
  • Count the number of digits in the input number. 
  • Initialize a variable (let’s call it “sum”) to store the sum of the cubes of the digits. 
  • Using a loop, extract each digit from the input number, cube it, and add it to the “sum” variable. 
  • After the loop finishes, compare the “sum” with the original input number. 
  • If the “sum” is equal to the input number, then the number is an Armstrong number; otherwise, it is not.
Fibonacci Number
Fibonacci Number
In mathematics, we have different types of numbers like integers, natural, real, rational, etc. Along with these, we have a beautiful number with a remarkable property known as a Fibonacci...read more
Fibonacci Program in C
Fibonacci Program in C
The Fibonacci Series is a fascinating sequence that appears in many areas of mathematics and science. The application of the Fibonacci Series can be seen in nature (growing or splitting...read more
Fibonacci Series in Java [Case-Study Based]
Fibonacci Series in Java [Case-Study Based]
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In Java, the Fibonacci...read more
Fibonacci Series Program in C++
Fibonacci Series Program in C++
The Fibonacci series is an intriguing mathematical idea that is popular for practicing in Python, especially for those just starting out. The Fibonacci series is an intriguing mathematical idea that...read more
Fibonacci Series in Python
Fibonacci Series in Python
Fibonacci Series is a sequence of numbers where each number is the sum of the two previous numbers. This article will discuss the Fibonacci Series, How to Create a Fibonacci...read more

Conclusion

An Armstrong Number is defined as the sum of the nth power of each digit to an n-digit number that is equal to that number. In this article, we have discussed different methods to check Armstrong numbers in C.

Hope you will like the article.

Keep Learning!!

Keep Sharing!!

Hello World Program in C
Hello World Program in C
Start your coding journey with the introductory program, i.e., Hello World Program in C. This is one of the most simple but powerful programs that give you the idea of...read more
10 Must-Read C Programming Books of All Time
10 Must-Read C Programming Books of All Time
This list of the top C programming books has been curated from several prestigious publications in computing literature, discussions on C programming communities across Reddit, Y Combinator, and bestsellers on...read more
C programming examples 
C programming examples 
If you want to learn C language by executing programming examples. Then this article offers you 17C programming examples with outputs, so that you can learn fast.
What is Interpreter: Types, Advantages and Disadvantages
What is Interpreter: Types, Advantages and Disadvantages
Interpreter is a type of computer program that directly executes instructions written in a programming or scripting language.
Learn About Array of Structure in C
Learn About Array of Structure in C
Discover how to use array structures in C to organize and store related data. Learn about creating, accessing, and manipulating arrays for efficient data management in your C programs. An...read more
Explore strtok() Function in C
Explore strtok() Function in C
The strtok() function in C is a useful tool for splitting strings into tokens based on a delimiter. With its various parameters, strtok() can help you efficiently parse and manipulate...read more
Guide to tolower() Function in C with Examples
Guide to tolower() Function in C with Examples
Enhance your C programming skills with our detailed guide on how to use toupper() function in C effectively. Enhance your C programming skills with our detailed guide on how to...read more
How to Reverse an Array in C
How to Reverse an Array in C
In C programming, Arrays are a useful data structure. It allows the storage of multiple values in a single variable instead of creating individual variables for each value. They are...read more
Learn to Implement strstr() Function in C
Learn to Implement strstr() Function in C
Discover how to use the Strstr function in the C programming language, including its syntax, parameters, and practical applications. Explore examples and tips to effectively implement Strstr in your code...read more
Concatenate Strings with strcat() Function in C
Concatenate Strings with strcat() Function in C
This article offers a comprehensive explanation of the syntax and implementation of the strcat() function, in addition to providing several examples to aid in understanding its operation. By the end...read more
Compare Strings with strcmp Function in C
Compare Strings with strcmp Function in C
The strcmp function in C is used to compare two strings and determine whether they are equal or not. This article explains the syntax and usage of the strcmp function...read more
Explore toupper() Function in C with Examples
Explore toupper() Function in C with Examples
The toupper() function takes a single character as input and returns the corresponding uppercase character. It is commonly used in conjunction with string manipulation functions to convert entire strings to...read more
All About Strlen() Function in C
All About Strlen() Function in C
Unveil the strlen() function in C programming, diving deep into its syntax, practical implementations, and code samples. Learn how to effectively utilise strlen() to determine string lengths, manipulate strings, and...read more
Understanding Strchr() Function in C
Understanding Strchr() Function in C
This blog explains the strchr() Function in C programming with the help of examples. Let’s understand!This blog explains the strchr() Function in C programming with the help of examples. Let’s...read more
strcpy() Function in C
strcpy() Function in C
Uncover the versatility of the strcpy() function in C programming, exploring its syntax, practical applications, and code examples. Learn how strcpy() efficiently copies strings, manipulate substrings, and master string operations...read more

FAQs

What is an Armstrong number?

An Armstrong number is a number that is equal to the sum of the cubes of its own digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Can you check for Armstrong number for negative or decimal numbers?

Armstrong number is only defined for positive integers, So, you can not check for negative or decimal numbers.

How to check Armstrong number for a large number?

The logic is the same as with small numbers, but you need to use a larger data type like long or Big Integer to hold the large number and result of the cubing and summing.

Is it necessary to check for the length of the input number?

Yes, it is necessary to check for the length of the input number, because the length of the input number is used to calculate the sum of cubes of the digits of the number.

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio