Question

I have written a code in PLSQL. Where in I need to Check if sum of cubes of digits = to number itself.

I have tried abiding by the algorithm,still there are some errors.Please help. I'm new to PLSQL.

Following is my code:

   set serveroutput on;



Declare 

    I number(4);
    Sum number(4):=0;
    C number(15):=10;   

Begin   
    for I in 1..999
    loop

    --      dbms_output.put_line(I);



        Sum:=power(mod(I,C),3);

        while mod(I,C)
        loop

            Sum:=power(mod(mod(I,C),C),3);


            C:=C*10;

        end loop;       


        if Sum=I then

            dbms_output.put_line(I);        

        end if;

    end loop;

End;

/
Was it helpful?

Solution

sum is a key word in plsql, you should not be using that as a variable name.

Here is the solution for your problem:

SET serveroutput ON format wraped;
DECLARE
  i    INTEGER := 153;
  j    INTEGER;
  summ INTEGER := 0;
BEGIN
  j      := i;
  WHILE i > 0
  LOOP
    summ := summ + MOD(i,10) ** 3;
    i    :=  FLOOR (i  / 10 );
  END LOOP;
  IF summ = j THEN
    dbms_output.put_line('Sum of cubes of digits is EQUAL to the number');
  ELSE
    dbms_output.put_line('Sum of cubes of digits is NOT EQUAL to the number');
  END IF;
END;

The solution works for any INTEGER, i, which is NUMBER(38).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top