C Program to Print Armstrong Number from 1 to 1000

«
»

This is a C Program to print armstrong number from 1 to 1000.

Problem Description

This C Program print armstrong number from 1 to 1000.

Problem Solution

An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself. Hence 153 because 13 + 53 + 33 = 1 + 125 + 27 = 153.

advertisement
Program/Source Code

Here is source code of the C Program to print armstrong number from 1 to 1000.
The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Print Armstrong Number from 1 to 1000
 */
#include <stdio.h>
 
main()
{
    int number, temp, digit1, digit2, digit3;
 
    printf("Print all Armstrong numbers between 1 and 1000:\n");
    number = 001;
    while (number <= 900)
    {
        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++;
    }
}
Program Explanation

In this C program, we are printing the Armstrong number from 1 to 1000. An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself. Hence, 153 because 13 + 53 + 33 = 1 + 125 + 27 = 153. We are initializing the number variable value as 001.

advertisement

Using while loop checks the condition that the value of ‘number’ variable is less than or equal to 900. If the condition is true then, execute the iteration of the loop. The ‘digit1’ variable is used to compute the division of the value of ‘number’ variable by 10 and multiply the resulting value by 10 and subtract the multiplied value with the value of ‘number’ variable.

The ‘digit2’ variable is used to compute the division of the value of ‘number’ variable by 100, multiply the resulting value with 10, divide the value of ‘number’ variable by 10 and subtract the multiplied value from the divided value.

The ‘digit3’ variable is used to compute the division of the value of ‘number’ variable by 100 and also by 1000, then multiply the value divided by 100 with 10 and subtract both the values. The ‘temp’ variable is used to multiply the value of digit1, digit2, digit3 variables to the power of 3 respectively. Compute the summation of all the three multiplied values. If condition statement is used to check the value of ‘temp’ and ‘number’ variables are equal, if the condition is true print the Armstrong number.

advertisement
Runtime Test Cases
 
Output:
$ cc pgm44.c
$ a.out
Print all Armstrong numbers between 1 and 1000:
 
Amstrong no is:1
Amstrong no is:153
Amstrong no is:370
Amstrong no is:371
Amstrong no is:407

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn