×

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base. For example, for a three digit number like 370 the numbers 3, 7, 0 when raised to the power of three and then added together will give 370 i.e. 370 = (3*3*3) + (7*7*7) + (0*0*0).

What is an Armstrong Number?

When the sum of a number’s digits raised to the power of the number of digits in the number equals the given number then it is referred to as an Armstrong number. There are a lot of Armstrong numbers.

Let’s take some examples of certain number base, for numbers with three digits there are only six Armstrong numbers in the range of 0-999 i.e. 0, 1, 153, 370, 371, 407 whereas for numbers with four digits there are three Armstrong numbers which are 1634, 8208, 9474 and there are many more Armstrong numbers with different number of digits.

XYZ = (x*x*x)+(y*y*y)+(z*z*z).

Let’s take an example from the above four digit Armstrong numbers and check if the number is an Armstrong number:

Armstrong Number in C

In the above example, we can see that when all the numbers of the digit 8208 are raised to the power of four (number base) are added together, they give 8208 (the number itself) as the result.

Algorithm of Armstrong number

The algorithm’s steps are given below:

Step 1: Get user input.

Step 2: Set the temporary variable (var = num) to the initial value of sum, which is set to zero.

Step 3: Next, determine how many digits there are overall in the provided number.

Step 4: Then, the total amount of digits are stored in a variable (n).

Step 5: Continue the loop until var>0.

Step 6: The results of while loop are stored in sum.

Step 7: Then, we check if the user number is equal to sum or not.

Step 8: If it is equal to the user number then print, “It is an Armstrong number”.

Step 9: And, if it is not then print, “It is not an Armstrong number”.

Flowchart to check a 3-digit Armstrong number:

Armstrong Number in C

Program in C to check whether a number is a Armstrong number or not

#include <math.h>
#include <stdio.h>
int main() {
   int n, Num, r, i = 0;
   float result = 0.0;
printf("Enter an integer: ");
   scanf("%d", &n);
Num = n;
   for (Num = n; Num != 0; ++i) {
       Num /= 10;
   }
   for (Num = n; Num != 0; Num /= 10) {
r = Num % 10;
      result += pow(r, i);
   }
   if ((int)result == n)
    printf("%d is an Armstrong number.", n);
   else
    printf("%d is not an Armstrong number.", n);
   return 0;
}

Output:

1. Enter an integer: 370
     370 is an Armstrong number.
2.  Enter an integer: 372
      372 is not an Armstrong number.

Therefore, with the help of the program given above, we can check if a given number is an Armstrong number or not. We can also make programs for specifically checking if a three digit number is an Armstrong number or not just by making some small changes to the algorithm and the code used for checking for an n digit number. When we don’t know the number of digits in a number before, just as explained in the algorithm in the program above, we follow all the steps to calculate the number of digits from the given user integer and store it.

In this program we initially calculate and store the integer's digit count in the variable i. Additionally, each time the second for loop iterates, the pow() (pow() function is  stored in the math.h header file included above) function is used to calculate the power of each unique digit. After this is done then with the use of if statement we check if it is an Armstrong number or not by providing it with a condition.


Related Topics

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Kruskal algorithm in C

Given a weighted graph, Kruskal's algorithm generates a spanning tree with the lowest possible weights. Start by creating an edge list for the given graph, including the weights. Sort the...

3 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.