while N !=0
LOOP
R:=MOD(N,10);
R1:=power(R,3);
A:=A+R1;
N:=TRUNC(N/10);
END LOOP;
After this it comes IF N=A THEN SYS.DBMS_OUTPUT.PUT_LINE(' number is armstrong '); ELSE SYS.DBMS_OUTPUT.PUT_LINE(' number is not an armstrong ');
while N !=0
LOOP
R:=MOD(N,10);
R1:=power(R,3);
A:=A+R1;
N:=TRUNC(N/10);
END LOOP;
After this it comes IF N=A THEN SYS.DBMS_OUTPUT.PUT_LINE(' number is armstrong '); ELSE SYS.DBMS_OUTPUT.PUT_LINE(' number is not an armstrong ');
Your problem is when you compare (N=A) because N is zero at the end of LOOP. Then you must compare A with the original number entered, (NOrig=A). This procedure you can help:
create or replace procedure amstrong_number(pNumber int)
is
NOrig int:=0;
N int:=0;
R int:=0;
R1 int:=0;
A int:=0;
begin
NOrig:=pNumber;
N:=pNumber;
WHILE N!= 0
LOOP
R:=MOD(N,10);
R1:=POWER(R,3);
A:=A+R1;
N:=TRUNC(N/10);
END LOOP;
IF NOrig = A THEN
dbms_output.put_line(' number is amstrong ');
ELSE
dbms_output.put_line(' number is not an amstrong ');
END IF;
end;
Regards
In a comment you say you want to take user input from the keyboard. It is not clear how you plan to do that; in general, PL/SQL does not have facilities for interaction with end users, it is a language for processing on the server.
One way to have the user provide a number is to have a procedure with an in parameter. Below is one example of how you can do it. You must compile the procedure first (for example, I saved it in a script, "Armstrong.sql", and then I ran the command "start Armstrong" in SQL*Plus). Then the user can use the procedure, which I called is_Armstrong, from SQL*Plus or a graphical interface like SQL Developer or Toad, like this: exec is_Armstrong(...) where in parentheses is the user's input number.
I built in two application exception checks - for negative or non-integer numbers and for numbers that are too long (more than 20 digits). I wrote it for Armstrong numbers of arbitrary length (<= 20), limiting it to three digits doesn't save almost any work so why not make it general. There are other possible application exceptions - for example, what if the user inputs a string instead of a number? I didn't handle all the errors - the very last example below shows an unhandled one. Write your own error handling code for whatever else you want to handle explicitly.
Content of Armstrong.sql:
create or replace procedure is_Armstrong(p_input_number number)
as
l_string varchar2(20);
l_sum number := 0;
l_loop_counter number;
l_len number;
not_a_positive_integer exception;
number_too_large exception;
begin
l_string := to_char(p_input_number);
l_len := length(l_string);
if p_input_number <= 0 or p_input_number != floor(p_input_number) then
raise not_a_positive_integer;
elsif l_len > 20 then
raise number_too_large;
end if;
for l_loop_counter in 1..l_len
loop
l_sum := l_sum + power(to_number(substr(l_string, l_loop_counter, 1)), l_len);
end loop;
if p_input_number = l_sum then
dbms_output.put_line('Number ' || l_string || ' is an Armstrong number.');
else
dbms_output.put_line('Number ' || l_string || ' is not an Armstrong number.');
end if;
exception
when not_a_positive_integer then
dbms_output.put_line('Input number must be a positive integer.');
when number_too_large then
dbms_output.put_line('Input number must be no more than twenty digits.');
end;
/
Compiling the procedure:
SQL> start Armstrong.sql
Procedure created.
Elapsed: 00:00:00.03
Usage examples:
SQL> exec is_Armstrong(-33);
Input number must be a positive integer
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.08
SQL> exec is_Armstrong(1.5);
Input number must be a positive integer
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.08
SQL> exec is_Armstrong(1234512345123451234512345);
Input number must be no more than twenty digits
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.01
SQL> exec is_Armstrong(3);
Number 3 is an Armstrong number.
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.00
SQL> exec is_Armstrong(100);
Number 100 is not an Armstrong number.
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.00
SQL> exec is_Armstrong(371);
Number 371 is an Armstrong number.
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.00
SQL> exec is_Armstrong('abc')
BEGIN is_Armstrong('abc'); END;
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 1
Elapsed: 00:00:00.00