C Program to Display Armstrong Number Between Two Intervals

@codevarsity
 · 
2.1k views
 · 

A simple C program to find all Armstrong numbers between two integers (entered by the user) using loops and if..else statement.

Related Topics

for loop  if..else statement 

Code

/*C Program to Display Armstrong Number Between Two Intervals */
#include <stdio.h>
#include <math.h>
int main()
{
    int low, high, i, temp1, temp2, remainder, n = 0, result = 0;

    printf("Enter two numbers(intervals): ");
    scanf("%d %d", &low, &high);
    printf("Armstrong numbers between %d an %d are: ", low, high);

    for(i = low + 1; i < high; ++i)
    {
        temp2 = i;
        temp1 = i;

        // number of digits calculation
        while (temp1 != 0)
        {
            temp1 /= 10;
            ++n;
        }

        // result contains sum of nth power of its digits
        while (temp2 != 0)
        {
            remainder = temp2 % 10;
            result += pow(remainder, n);
            temp2 /= 10;
        }

        // checks if number i is equal to the sum of nth power of its digits
        if (result == i) {
            printf("%d ", i);
        }

        // resetting the values to check Armstrong number for next iteration
        n = 0;
        result = 0;

    }
    return 0;
}

Output

Enter two numbers(intervals): 999
99999
Armstrong numbers between 999 an 99999 are: 1634 8208 9474 54748 92727 93084

Explanation

C Program to Add Two Complex Numbers by Passing Structure to a Function

In the structure complex, we are using float datatype for variables, real and imag since our concern is to add any two complex numbers with a possible fractional value.

In the main() function, the real part and the imaginary part of the complex numbers are taken separtely using scanf().

The structures n1 and n2 are passed as an argument of function add(). The function computes the sum and returns the structure variable temp to the main() function.

The final result is printed using printf() throught the main() function.

Tags

#for  #loop  #return 

Comments