If the sum of the cubes of individual digits of a number is equal to that number, it is an Armstrong number. WITH ArmStrong AS( SELECT 0 AS Num UNION ALL SELECT Num+1 FROM ArmStrong WHERE Num < 999 ) SELECT ArmstrongNumber = Num FROM ArmStrong WHERE Num = POWER(COALESCE(SUBSTRING(cast(Num AS VARCHAR(10)),1,1),0),3) + POWER(COALESCE(SUBSTRING(cast(Num AS VARCHAR(10)),2,1),0),3) + POWER(COALESCE(SUBSTRING(cast(Num AS VARCHAR(10)),3,1),0),3) OPTION(MAXRECURSION 0)
How do we insert the results of the stored procedure in a table?
ReplyDeletecan we generate An Armstrong number using SQL script?
ReplyDeleteIf the sum of the cubes of individual digits of a number is equal to that number, it is an Armstrong number.
DeleteWITH ArmStrong AS(
SELECT
0 AS Num
UNION ALL
SELECT
Num+1
FROM ArmStrong
WHERE Num < 999
)
SELECT
ArmstrongNumber = Num
FROM ArmStrong
WHERE
Num = POWER(COALESCE(SUBSTRING(cast(Num AS VARCHAR(10)),1,1),0),3)
+ POWER(COALESCE(SUBSTRING(cast(Num AS VARCHAR(10)),2,1),0),3)
+ POWER(COALESCE(SUBSTRING(cast(Num AS VARCHAR(10)),3,1),0),3)
OPTION(MAXRECURSION 0)