×

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

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.