Monday, 2 July 2012

Armstrong_numbers_1 to 500 in C














Code -



//_________Program to print the armstrong numbers___________

/* If sum of cubes of each digit of the number is equal to the number itself,

then the number is called an Armstrong number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) +

 ( 3 * 3 * 3 ) */

//______________Including Header Files______________

#include<stdio.h>

#include<conio.h>

//_________Mainly included for the getch() function______________

int main()
{
int number = 0, counter = 0, remainder = 0, number_cube = 0;

//_____Variable declaration and Initialization_____

printf("\n\n     ______ Program will print Armstrong numbers between 1 and 500 ______");

printf("\n\n\t\t   ARMSTRONG NUMBERS - ");

for( counter = 0; counter <= 500; counter++)
{
number = counter;

/*__________Copying the number to be checked in another variable so that value of counter does

not change during digit extraction________*/

number_cube = 0;

//__________Setting the sum of the cube of digits to 0____________

/* While loop to extract digits, calculate their cube and find out the sum of all the digit cubes */

while(number != 0)
{
remainder = number % 10;

number = number / 10;

number_cube = number_cube + remainder * remainder * remainder;
}

if (number_cube == counter)

/*________Checking if sum of the cube of digits is equal to the original number__________*/

{
printf("%d ", counter);

//________Displaying the Armstrong Number____________
}

}

getch();

return 0;

/*_____return 0 will tell the compiler that the program has executed successfully without any

error______*/
}


Follow us on Facebook - http://www.facebook.com/pages/Assignment-Hub/388378154532099

No comments:

Post a Comment