Write a pl/sql program to print the sum of first 10 prime numbers
Answers
Answered by
10
Prime number in PL/SQL
Prerequisite – PL/SQL introduction
A prime number is a whole number greater than 1, which is only divisible by 1 and itself. First few prime numbers are : 2 3 5 7 11 13 17 19 23 …..
In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements.
In declare part, we declare variables and between begin and end part, we perform the operations.
Examples:
Input : 5 Output : true Input : 10 Output : false
Below is the required implementation:
declare
-- declare variable n, i
-- and temp of datatype number
n number;
i number;
temp number;
begin
-- Here we Assigning 10 into n
n := 13;
-- Assigning 2 to i
i := 2;
-- Assigning 1 to temp
temp := 1;
-- loop from i = 2 to n/2
for i in 2..n/2
loop
if mod(n, i) = 0
then
temp := 0;
exit;
end if;
end loop;
if temp = 1
then
dbms_output.put_line('true');
else
dbms_output.put_line('false');
end if;
end;
-- Program End
.
Prerequisite – PL/SQL introduction
A prime number is a whole number greater than 1, which is only divisible by 1 and itself. First few prime numbers are : 2 3 5 7 11 13 17 19 23 …..
In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements.
In declare part, we declare variables and between begin and end part, we perform the operations.
Examples:
Input : 5 Output : true Input : 10 Output : false
Below is the required implementation:
declare
-- declare variable n, i
-- and temp of datatype number
n number;
i number;
temp number;
begin
-- Here we Assigning 10 into n
n := 13;
-- Assigning 2 to i
i := 2;
-- Assigning 1 to temp
temp := 1;
-- loop from i = 2 to n/2
for i in 2..n/2
loop
if mod(n, i) = 0
then
temp := 0;
exit;
end if;
end loop;
if temp = 1
then
dbms_output.put_line('true');
else
dbms_output.put_line('false');
end if;
end;
-- Program End
.
Answered by
53
set serveroutput on;
declare
counter number;
k number;
num number;
sums number;
begin
num := 0;
sums:=0;
for n in 1..50 loop
counter := 0;
k := floor(n/2);
for i in 2..k loop
if (mod(n, i) = 0 ) then
counter := 1;
end if;
end loop;
if (counter = 0) then
sums := sums+n;
num := num + 1;
end if;
if (num = 10)
then
dbms_output.put_line(sums);
exit;
end if;
end loop;
end;
Similar questions