In: Computer Science
Write a PL/SQL program that prints following: if sum of its digits raised to the power n is equal to number itself, where n is total digits in number.
If the Input is: 129, then output of the program will be: 738
Hi,
By the question I understood that you are looking for Armstrong numer check.
Below is the code which takes user input and prints the sum of the digits to the power of the number of digits and it also prints if the given number is Armstrong number or not
declare
n number;
s number:=0;
r number;
len number;
m number;
begin
n := &input;
m := n;
len := length(to_char(n));
-- while loop till n>0
while n>0
loop
r := mod(n , 10);
s := s + power(r , len);
n := trunc(n / 10);
end loop;
dbms_output.put_line('output is : ' || s);
if m = s
then
dbms_output.put_line('Armstrong number');
else
dbms_output.put_line('Not armstrong number');
end if;
end;
Below are the screeenshots:
The above program will ask for the input number.