- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 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
- Related Articles
- Count even and odd digits in an Integer in C++
- Reverse a Number in PL/SQL
- Count no. of characters and words in a string in PL/SQL
- Sum of individual even and odd digits in a string number using JavaScript
- Count number of even and odd elements in an array in C++
- Find the sum of digits of a number at even and odd places in C++
- Count number of ordered pairs with Even and Odd Product in C++
- Count number of ordered pairs with Even and Odd Sums in C++
- Difference between SQL and PL/SQL
- Print all odd numbers and their sum from 1 to n in PL/SQL
- Difference between sums of odd and even digits.
- Difference Between T-SQL and PL-SQL
- Sum of the first and last digit of a number in PL/SQL
- Python program to Count Even and Odd numbers in a List
- Check if product of digits of a number at even and odd places is equal in Python
