Count odd and even digits in a number in PL/SQL

We are given a positive integer of digits and the task is to calculate the count of odd and even digits in a number using PL/SQL.

PL/SQL is a combination of SQL along with the procedural features of programming languages. It was developed by Oracle Corporation in the early 90's to enhance the capabilities of SQL.

PL/SQL is one of three key programming languages embedded in the Oracle Database, along with SQL itself and Java.

Input

int number = 23146579

Output

count of odd digits in a number are : 5
count of even digits in a number are : 3

Explanation − In the given number, we have 2, 4, 6 as even digits therefore count of even digits in a number are 3 and we have 3, 1, 5, 7 and 9 as odd digits therefore count of odd digits in a number are 5.

Input

int number = 4567228

Output

count of odd digits in a number are : 2
count of even digits in a number are : 5

Explanation − In the given number, we have 5 and 7 as odd digits therefore count of odd digits in a number are 2 and we have 4, 6, 2, 2 and 8 as even digits therefore count of even digits in a number are 5.

Approach used in the below program is as follows

  • Input a number in an integer type variable of datatype NUMBER used in PL/SQL.

  • Take a length of type VARCHAR2(50) which describes the maximum size length can store.

  • Take two variables as count for odd digits and count for even digits and initially set them to 0

  • Start Loop For from 1 till the length while passing a number to it

  • Inside the loop, set length as substr(number, i, 1) to extract individual digits

  • Now, check IF mod of length by 2 is not equals to 0 then increase the count for odd digits in a number

  • Else, increase the count of even digits in a number

  • Print the result using DBMS_OUTPUT.PUT_LINE

Example

The following PL/SQL block demonstrates how to count odd and even digits in a number ?

DECLARE
   digits NUMBER := 23146579;
   length VARCHAR2(50);
   count_odd NUMBER(10) := 0;
   count_even NUMBER(10) := 0;
BEGIN
   FOR i IN 1..Length(digits)
   LOOP
      length := Substr(digits, i, 1);
      IF mod(length, 2) != 0 THEN
         count_odd := count_odd + 1;
      ELSE
         count_even := count_even + 1;
      END IF;
   END LOOP;
   dbms_output.Put_line('count of odd digits in a number are : ' || count_odd);
   dbms_output.Put_line('count of even digits in a number are : ' || count_even);
END;
/

Output

If we run the above code it will generate the following output −

count of odd digits in a number are : 5
count of even digits in a number are : 3

Alternative Example with Different Input

Let's test the same logic with a different number ?

DECLARE
   digits NUMBER := 4567228;
   length VARCHAR2(50);
   count_odd NUMBER(10) := 0;
   count_even NUMBER(10) := 0;
BEGIN
   FOR i IN 1..Length(digits)
   LOOP
      length := Substr(digits, i, 1);
      IF mod(length, 2) != 0 THEN
         count_odd := count_odd + 1;
      ELSE
         count_even := count_even + 1;
      END IF;
   END LOOP;
   dbms_output.Put_line('count of odd digits in a number are : ' || count_odd);
   dbms_output.Put_line('count of even digits in a number are : ' || count_even);
END;
/

The output of the above code is ?

count of odd digits in a number are : 2
count of even digits in a number are : 5

This PL/SQL approach efficiently counts odd and even digits by converting the number to string format and checking each digit individually using the modulo operator. The solution works for any positive integer of any length.

Updated on: 2026-03-14T20:56:32+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements