Skip to content
Related Articles

Related Articles

C Program to Display Armstrong Number Between Two Intervals

View Discussion
Improve Article
Save Article
  • Last Updated : 01 Aug, 2022
View Discussion
Improve Article
Save Article

To display the Armstrong number between two intervals we can use 2 different Methods with 4 approaches:

  1. Without using the pow() function
  2. Using pow() function

We will keep the same input in all the mentioned approaches and get an output accordingly.

Input: 

start = 1, end = 500 

Output: 

1
153
370
371
407 

Explanation: The Armstrong number is the sum of the cubes of its digits.

i.e. 1=13 
153= 13 + 53 + 3 
370= 33 + 73 + 03 etc. 

Method 1: Without using the pow() function

Approach A:

C




// C program to demonstrate an armstrong number
// between the given intervals
  
#include <stdio.h>
  
int main()
{
    int s = 1, e = 500, num, n, arm = 0, i, sum;
    
    // iterating the for loop 
    // using the given intervals
    for (i = s; i <= e; i++) {
        num = i;
        sum = i;
        
        // finding the armstrong number
        while (num != 0) {
            n = num % 10;
            arm = arm + (n * n * n);
            num = num / 10;
        }
        
        // if number is equal to 
        // the arm then it is a
        // armstrong number
        if (sum == arm) {
            printf("%d\n", i);
        }
        arm = 0;
    }
    return 0;
}

Output

1
153
370
371
407

Approach B:

C




// C program to demonstrate an armstrong number
// between the given intervals
#include <stdio.h>
  
int main()
{
  
    int s = 1, e = 500, num1, n, arm = 0, i, num2, c;
    
    // iterating the for loop using the given intervals
    for (i = s; i <= e; i++) {
        
        num1 = i;
        num2 = i;
        
        // finding the number of digits
        while (num1 != 0) {
            num1 = num1 / 10;
            ++c;
        }
        
        // finding the armstrong number
        while (num2 != 0) {
            n = num2 % 10;
            arm = arm + (n * n * n);
            num2 = num2 / 10;
        }
        
        // if number is equal to the arm then it is a
        // armstrong number
        if (arm == i) {
            printf("%d\n", i);
        }
        arm = 0;
        c = 0;
    }
    return 0;
}

Output

1
153
370
371
407

Method 2: Using pow() function: 

Approach A: 

C




// C program to demonstrate an armstrong number
// between the given intervals using pow()
  
#include <math.h>
#include <stdio.h>
int main()
{
  
    int s = 1, e = 500, num, n, arm = 0, i, sum;
    // iterating the for loop using the given intervals
    for (i = s; i <= e; i++) {
        num = i;
        sum = i;
        // finding the armstrong number
        while (num != 0) {
            n = num % 10;
            arm = arm + pow(n, 3);
            num = num / 10;
        }
        // if number is equal to the arm then it is a
        // armstrong number
        if (sum == arm) {
            printf("%d\n", i);
        }
        arm = 0;
    }
    return 0;
}

Output

1
153
370
371
407

Approach B: 

C




// C program to demonstrate an armstrong number
// between the given intervals using pow()
  
#include <math.h>
#include <stdio.h>
int main()
{
  
    int s = 1, e = 500, num1, n, arm = 0, i, num2, c;
    // iterating the for loop using the given intervals
    for (i = s; i <= e; i++) {
        num1 = i;
        num2 = i;
        // finding the number of digits
        while (num1 != 0) {
            num1 = num1 / 10;
            ++c;
        }
        // finding the armstrong number
        while (num2 != 0) {
            n = num2 % 10;
            arm = arm + pow(n, 3);
            num2 = num2 / 10;
        }
        // if number is equal to the arm then it is a
        // armstrong number
        if (arm == i) {
            printf("%d\n", i);
        }
        arm = 0;
        c = 0;
    }
    return 0;
}

Output

1
153
370
371
407

  


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!