Monday, July 27, 2009

Program for printing amstrong numbers within 1000 using c language?

give complete program details using c language

Program for printing amstrong numbers within 1000 using c language?
PROGRAM ArmstrongNumber


IMPLICIT NONE





INTEGER :: a, b, c ! the three digits


INTEGER :: abc, a3b3c3 ! the number and its cubic sum


INTEGER :: Count ! a counter





Count = 0


DO a = 0, 9 ! for the left most digit


DO b = 0, 9 ! for the middle digit


DO c = 0, 9 ! for the right most digit


abc = a*100 + b*10 + c ! the number


a3b3c3 = a**3 + b**3 + c**3 ! the sum of cubes


IF (abc == a3b3c3) THEN ! if they are equal


Count = Count + 1 ! count and display it


WRITE(*,*) 'Armstrong number ', Count, ': ', abc


END IF


END DO


END DO


END DO





END PROGRAM ArmstrongNumber
Reply:int cube(int n)


{


return n*n*n;


}





main()


{


int i;


for(i=0;i%26lt;1000;i++)


{


if ( i == cube(i%10) + cube((i/10)%10) + cube((i/100)%10))


{


printf("%d\n", i);


}


}


}
Reply:here is the program to print armstrong numbers from 1 to 1000





#include%26lt;stdio.h%26gt;


main()


{


int a,b,c,d;


for(a=1;a%26lt;=1000;a++)


{


c=a;


d=0;


while(a!=0)


{


b=a%10;


a=a/10;


d=(b*b*b)+d;


}


a=c;


if(d==c)


printf("\narmstrong number %d\n",c);





}
Reply:#include%26lt;stdio.h%26gt;





void main()


{


int i=0, a=0,b=0,c=0,sum=0;





for(i=0;i%26lt;999;i++)


{


sum=0;


a=i%10; /* Units Digit */


b=(i%100-a)/10; /* Tenths digit */


c=(i%1000-(b*10+a))/100; /*100ths Digit*/


sum= (a*a*a) + (b*b*b) + (c*c*c);


if(i==sum)


printf("Armstong Number: %d\n",sum);


}





}
Reply:The asker asked for it in C.





Check out this link - it has a version in C


No comments:

Post a Comment