Friday, June 6, 2008

Print Armstrong numbers between 1- 1000

Write a program to print out all Armstrong numbers between 1 and 1000.If sum of cubes of each digit of the number is equal to number itself,then the number is said to be "Armstrong no."

For example 153 = ( 1*1*1) + (5*5*5) + (3 *3*3)

Soln Code :-
========

#include <stdio.h>

int main(void)

{

int i = 0 ;
while ( i <= 999 )
{
int a,b,c,d = 0 ;
a = (i /100);
b = (( i % 100) / 10);
c = (( i % 100) % 10);
d = ((a * a * a) + ( b * b * b) + (c * c * c));
if ( d == i)
printf("%d is Armstrong number\n ",i);
i = i++ ;
}
}

*Second way using for *
===================
#include <stdio.h>

int main(void)
{
int i ,a = 0,b = 0,c = 0 ,d = 0;
for ( i = 1 ; i <= 1000 ;i++ )
{
a = (i / 100);
b = (( i / 10 ) % 10 );
c = ((i % 100 ) % 10 );
d = (( a * a * a ) + (b * b * b) + ( c * c * c));
if ( i == d )
printf(" %d is Armstrong number\n",i );
}
}













No comments: