SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
To count the odd and even digits in a number, you can iterate through each digit of the number and check if it's even or odd. Here's a PL/SQL block to count odd and even digits of a given number:
DECLARE num NUMBER := 123456; -- Example number digit NUMBER; odd_count NUMBER := 0; even_count NUMBER := 0; BEGIN WHILE num > 0 LOOP -- Get the last digit digit := MOD(num, 10); -- Check if digit is odd or even IF MOD(digit, 2) = 0 THEN even_count := even_count + 1; ELSE odd_count := odd_count + 1; END IF; -- Remove the last digit num := TRUNC(num / 10); END LOOP; -- Output DBMS_OUTPUT.PUT_LINE('Number of even digits: ' || even_count); DBMS_OUTPUT.PUT_LINE('Number of odd digits: ' || odd_count); END; /
In this PL/SQL block, we use the MOD
function to get the remainder when dividing by 10, which gives us the last digit. The TRUNC
function is used to remove the last digit. The loop continues until there are no more digits left in the number.
PL/SQL program to count odd and even digits in a number:
CREATE OR REPLACE PROCEDURE count_odd_even_digits(p_number NUMBER) IS v_digit NUMBER; v_odd_count NUMBER := 0; v_even_count NUMBER := 0; BEGIN WHILE p_number > 0 LOOP v_digit := MOD(p_number, 10); IF MOD(v_digit, 2) = 0 THEN v_even_count := v_even_count + 1; ELSE v_odd_count := v_odd_count + 1; END IF; p_number := TRUNC(p_number / 10); END LOOP; DBMS_OUTPUT.PUT_LINE('Odd digit count: ' || v_odd_count); DBMS_OUTPUT.PUT_LINE('Even digit count: ' || v_even_count); END count_odd_even_digits; /
Oracle SQL function for counting odd and even digits:
CREATE OR REPLACE FUNCTION count_odd_even_digits_sql(p_number NUMBER) RETURN VARCHAR2 IS v_digit NUMBER; v_odd_count NUMBER := 0; v_even_count NUMBER := 0; BEGIN WHILE p_number > 0 LOOP v_digit := MOD(p_number, 10); IF MOD(v_digit, 2) = 0 THEN v_even_count := v_even_count + 1; ELSE v_odd_count := v_odd_count + 1; END IF; p_number := TRUNC(p_number / 10); END LOOP; RETURN 'Odd digit count: ' || v_odd_count || ', Even digit count: ' || v_even_count; END count_odd_even_digits_sql; /
Counting odd and even digits using PL/SQL:
DECLARE v_number_to_analyze NUMBER := 123456789; v_digit NUMBER; v_odd_count NUMBER := 0; v_even_count NUMBER := 0; BEGIN WHILE v_number_to_analyze > 0 LOOP v_digit := MOD(v_number_to_analyze, 10); IF MOD(v_digit, 2) = 0 THEN v_even_count := v_even_count + 1; ELSE v_odd_count := v_odd_count + 1; END IF; v_number_to_analyze := TRUNC(v_number_to_analyze / 10); END LOOP; DBMS_OUTPUT.PUT_LINE('Odd digit count: ' || v_odd_count); DBMS_OUTPUT.PUT_LINE('Even digit count: ' || v_even_count); END; /
PL/SQL procedure for odd and even digit analysis:
CREATE OR REPLACE PROCEDURE analyze_digits(p_number NUMBER) IS v_digit NUMBER; v_odd_count NUMBER := 0; v_even_count NUMBER := 0; BEGIN WHILE p_number > 0 LOOP v_digit := MOD(p_number, 10); IF MOD(v_digit, 2) = 0 THEN v_even_count := v_even_count + 1; ELSE v_odd_count := v_odd_count + 1; END IF; p_number := TRUNC(p_number / 10); END LOOP; DBMS_OUTPUT.PUT_LINE('Odd digit count: ' || v_odd_count); DBMS_OUTPUT.PUT_LINE('Even digit count: ' || v_even_count); END analyze_digits; /
Oracle SQL query to find odd and even digit count:
SELECT SUM(CASE WHEN MOD(digit, 2) = 0 THEN 1 ELSE 0 END) AS even_digit_count, SUM(CASE WHEN MOD(digit, 2) <> 0 THEN 1 ELSE 0 END) AS odd_digit_count FROM ( SELECT SUBSTR(TO_CHAR(your_column), LEVEL, 1) AS digit FROM dual CONNECT BY LEVEL <= LENGTH(TO_CHAR(your_column)) );
How to calculate odd and even digits in PL/SQL:
DECLARE v_number_to_analyze NUMBER := 987654321; v_digit NUMBER; v_odd_count NUMBER := 0; v_even_count NUMBER := 0; BEGIN WHILE v_number_to_analyze > 0 LOOP v_digit := MOD(v_number_to_analyze, 10); IF MOD(v_digit, 2) = 0 THEN v_even_count := v_even_count + 1; ELSE v_odd_count := v_odd_count + 1; END IF; v_number_to_analyze := TRUNC(v_number_to_analyze / 10); END LOOP; DBMS_OUTPUT.PUT_LINE('Odd digit count: ' || v_odd_count); DBMS_OUTPUT.PUT_LINE('Even digit count: ' || v_even_count); END; /