C Program To Display Armstrong Number Between Two Intervals

In this post, we will learn how to display Armstrong Number between two intervals using C Programming language.

C Program To Display Armstrong Number Between Two Intervals

As we all know, Armstrong Number is a number which is equal to the sum of cubes of its digits. 
For Example: 370 = (3) + (7) +(0) = 27 + 343 + 0 = 370

In this program, we will find all the Armstrong Numbers lying between two integers.

So, without further ado, let’s begin the tutorial.

C Program To Display Armstrong Number Between Two Intervals

// C Program To Display Armstrong Number Between Two Intervals
#include <stdio.h>
#include <math.h>

int main(){
    int first, last, temp1, temp2, remainder, i, num = 0, result = 0;
    
    // Asking for Input
    printf("Enter First Value: ");
    scanf("%d", &first);
    printf("Enter Last Value: ");
    scanf("%d", &last);
    
    // logic
    for (i = first; i < last; ++i){
        temp1 = i;
        temp2 = i;
        
        while (temp1 != 0){
            temp1 = temp1 / 10;
            ++num;
        }
        while (temp2 != 0){
            remainder = temp2 % 10;
            result = result + pow(remainder, num);
            temp2 = temp2 / 10;
        }
        if (result == i){
            printf("%d\n", i);
        }
        num = 0;
        result = 0;
    }
    return 0;
}

Output

Enter First Value: 100
Enter Last Value: 1000
153
370
371
407

How Does This Program Work ?

    int first, last, temp1, temp2, remainder, i, num = 0, result = 0;

In this program, we have declared several int data type variables named as first, last, temp1, temp2, remainder, num and result. 

    // Asking for Input
    printf("Enter First Value: ");
    scanf("%d", &first);
    printf("Enter Last Value: ");
    scanf("%d", &last);

Then, the user is asked to enter the first and last digit of the interval. The starting integer of the interval is stored in the first named variable while the last integer is stored in the last named variable.

    for (i = first; i < last; ++i){
        temp1 = i;
        temp2 = i;
        
        while (temp1 != 0){
            temp1 = temp1 / 10;
            ++num;
        }

We repeat the process until the temporary number is not equal to 0. Then, we divide the temp1 by 10 and increase the value of num.

        if (result == i){
            printf("%d\n", i);
        }
        num = 0;
        result = 0;
    }

If the value of the result is equal to the value of the number, then it is an Armstrong Number. 

We will repeat this process until the last number is encountered.

Some of the used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program. 

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively. 

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.

scanf() – scanf() function is used to take input from the user.  

printf() – printf() function is used to display and print the string under the quotation to the screen.  

for loop – A loop is used for initializing a block of statements repeatedly until a given condition returns false.

while Loop – In the while loop, the statements inside the body of the while loop keeps executing until it is evaluated to False.

% – It is known as Modulus Operator and provides remainder after division.

/ – It divides numerator by denominator and returns the quotient. 

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True. 

pow() pow() function is used to calculate the power raise to the base value.

// – Used for Commenting in C.

Conclusion

I hope after going through this post, you understand how to display Armstrong numbers between two intervals Using C Programming language.

If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *