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 an even digits therefore count of even digits in a number are 3 and we have 3, 1, 5, 7 and 9 as an 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 an odd digits therefore count of odd digits in a number are 2 and we have 4, 6, 2, 2 and 8 as an 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 VARCHAR(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 pass a number to it
Inside the loop, set length as substr(number, i, 1)
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.
Example
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