Sei sulla pagina 1di 2

!

!
!
!
!
!
!

--------------------------------------------------------------This program computes all Armstrong numbers in the range of


0 and 999. An Armstrong number is a number such that the sum
of its digits raised to the third power is equal to the number
itself. For example, 371 is an Armstrong number, since
3**3 + 7**3 + 1**3 = 371.
---------------------------------------------------------------

PROGRAM ArmstrongNumber
IMPLICIT NONE
INTEGER :: a, b, c
INTEGER :: abc, a3b3c3
INTEGER :: Count

! the three digits


! the number and its cubic sum
! 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
----------------------------------------------------------------------------------------------------------------#include<stdio.h>
main()
{
int number, temp, digit1, digit2, digit3;
printf("Printing all Armstrong numbers between 1 and 500:\n\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("\nAmstrong Number:%d", temp);
}
number++;

}
}
---------------------------------------------------------------------------------------------------------------C# Code
long n, q, r, s, i;
Console.WriteLine("Enter the Number :"); n = Convert.ToInt64(Console.ReadLine())
;//Printf("enter the Number.");Scanf("%d",&n);
for (i = 0; i <= n; i++)
{ q = i;
s = 0;
while (q > 0)
{ r = q % 10;
s = s + r * r * r;
q = q / 10;
}
if (i == s)
Console.WriteLine("\n"+i+"\n");//This line says to Print 'i' to get desired Reus
lt.
} Console.ReadLine();//getch();

Potrebbero piacerti anche