先决条件 – PL/SQL简介 在PL/SQL代码中,命令组排列在一个块中。与声明或语句相关的块组。我们在部分中声明变量,在部分中声明变量,在部分中执行变量。
null
给定一个数字,任务是求该数字的位数和。 例如:
Input: 123456 Output: 21 Input: 9874 Output: 28
方法 就是取一个数字,用MOD函数得到每个数字,然后求和。
以下是所需的实施:
DECLARE --Declare variable n, temp_sum -- and r of datatype number n INTEGER ; temp_sum INTEGER ; r INTEGER ; BEGIN n := 123456; temp_sum := 0; -- here we check condition with the help of while loop -- here <> symbol represent for not null WHILE n <> 0 LOOP r := MOD(n, 10); temp_sum := temp_sum + r; n := Trunc(n / 10); END LOOP; dbms_output.Put_line( 'sum of digits = ' || temp_sum); END ; -- Program End |
输出:
sum of digits = 21
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END