C Program to display all Armstrong numbers between 1 and 500 - IProgramX

Q. Write a program to display all Armstrong numbers between 1 and 500. (An Armstrong number is a number such that the sum of cube of digits = number itself Ex. 153 = 1*1*1 + 5*5*5+ 3*3*3 


Program

#include <stdio.h>
main()
{
    int number, temp, digit1, digit2, digit3;

    printf("Print all Armstrong numbers between 1 and 500:\n");
    number = 001;
    while (number <= 500)
    {
        digit1 = number - ((number / 10) * 10);
        digit2 = (number / 10) - ((number / 100) * 10);
        digit3 = (number / 100) - ((number / 1000) * 10);
        temp = (digit1 * digit1 * digit1) + (digit2 * digit2 * digit2) + (digit3 * digit3 * digit3);
        if (temp == number)
        {
            printf("\n Armstrong no is:%d", temp);
        }
        number++;
    }
}

Output:

Print all Armstrong numbers between 1 and 500:

 Armstrong no is:1
 Armstrong no is:153
 Armstrong no is:370
 Armstrong no is:371
 Armstrong no is:407

Post a Comment

0 Comments