Tech Study

Armstrong number in C Language between 1 to n Numbers

To write the program to identify the armstrong number using c language first we need to understand what is an Armstrong Number. A number will be called armstrong number if the sum of cube of its digits is equal to the number itself.

For example 0, 1, 153, 370, 371 and 407 are armstrong number

Take 153;
Cube of 1 is 1, cube of 5 is 125 and cube of 3 is 27 and sum of 1+125+27 is 153.

407 = 43 + 03 + 73 = 64 + 0 + 343 = 407

It aplies to to other numbers. in this article we will learn to write a program to Armstrong Number in c programming language.

Write a program to Indentify Armstrong number in C Language

Write C program to find Armstrong numbers between 1 to n

What is Armstrong number?

An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.

For Example: 

#include <stdio.h>
#include <math.h> //defines common mathematical functions

int main()
{
    int lower, higher, i, temp1, temp2, remainder, n = 0, result = 0;

    printf("Enter two numbers: ");
    //Reading numbers from user
    scanf("%d %d", &lower, &higher);

    printf("Armstrong numbers between %d an %d are: ", lower, higher);
    for(i = lower + 1; i < higher; ++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;
        }

        // checking 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;
}

Result

Write C program to find Armstrong numbers between 1 to n
Write C program to find Armstrong numbers between 1 to n

Check this

  1. Write C Program to check prime and armstrong numbers using function

TaggedWrite C program to find Armstrong numbers between 1 to n

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more