In Dart programming, identifying whether a number is even or odd is a fundamental task often encountered when developing logical or arithmetic-based applications. Understanding how to check for even and odd numbers is essential, especially for beginners.
This post explains we’ll explore how to check for even and odd numbers in Dart, including examples and explanations.
Even and Odd Numbers in Dart Programming
Getting Started
Checking whether a number is even or odd in Dart is straightforward and can be done using the modulus operator %. With Dart's concise syntax and support for functional programming features like .where(), it's easy to filter and process numeric data efficiently. Understanding these basics is a building block for more complex logical operations in your Dart or Flutter applications.
What are Even and Odd Numbers?
Before diving into code, let's clarify the definitions:- Even numbers are divisible by 2 without a remainder (e.g., 0, 2, 4, 6, 8...).
- Odd numbers are not divisible by 2 evenly; they leave a remainder of 1 (e.g., 1, 3, 5, 7, 9...).
void main() {
int number = 7;
if (number % 2 == 0) {
print('$number is even');
} else {
print('$number is odd');
}
}
Explanation
- number % 2 calculates the remainder of dividing number by 2.
- If the remainder is 0, the number is even.
- Otherwise, it's odd.
Filter Odd & Even Digits from Number
To filter odd and even digits from a number using arithmetic in Dart, you can extract each digit using modulo (%) and integer division (~/).
Dart Code
void main() {
int number = 4234098;
int orgnumber=number;
String ever_numbers='';
String odd_numbers='';
int digit=0;
while(number!=0){
digit += number % 10;
if (digit % 2 == 0) {
ever_numbers+=' '+digit.toString();
}
else {
odd_numbers+=' '+digit.toString();
}
number ~/= 10; // Integer division
}
print('Odd digits are $odd_numbers of number : $orgnumber'); print('Even digits are $ever_numbers of number : $orgnumber');
}
Output
Odd digits are 17 17 21 of number : 4234098
Even digits are 8 24 26 30 of number : 4234098
Explanation:
% 10
gets the last digit.~/ 10
removes the last digit.% 2 == 0
checks if digit is even.- Based on output the even number and odd number
(digits) are conconated to
ever_numbers
orodd_numbers
string variable
Filter Odd & Even Digits from Number Using String
To filter odd and even digits from a number in Dart, you can convert the number to a string, split it into characters (digits), then parse and filter them using .isEven and .isOdd.
Dart Codevoid main() {
int number = 1234567890;
// Convert number to list of digits
List<int> digits = number
.toString()
.split('')
.map(int.parse)
.toList();
// Filter even and odd digits
List<int> evenDigits = digits.where((d) => d.isEven).toList();
List<int> oddDigits = digits.where((d) => d.isOdd).toList();
print('Even Digits: $evenDigits');
print('Odd Digits: $oddDigits');
}
Output
Even Digits: [2, 4, 6, 8, 0]
Odd Digits: [1, 3, 5, 7, 9]
Explanation:
toString().split('')
: Converts number to a list of digit characters..map(int.parse)
: Converts each character to an integer.isEven
/isOdd
: Checks the parity of each digit.
Filter Even and Odd Numbers in a List
In Dart, you can filter even and odd numbers from a list using the .where() method combined with a condition. Here's a simple example:
Dart Code void main() {
List<int> input_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
List<int> even_numbers = input_numbers.where((n) => n % 2 == 0).toList();
List<int> odd_numbers = input_numbers.where((n) => n % 2 != 0).toList();
print('Even Numbers: $even_numbers');
print('Odd Numbers: $even_numbers');
}
Output
Even Numbers: [2, 4, 6, 8, 10]
Odd Numbers: [2, 4, 6, 8, 10]
Explanation:
n % 2 == 0
checks if the number is even.n % 2 != 0
checks if the number is odd..where(...)
filters elements based on the condition..toList()
converts the result back to aList
.
Checking Odd and Even Numbers using isEven
and isOdd
Properties
Great! Dart provides built-in properties isEven
and isOdd
on int values, which makes checking for even or odd numbers very clean and readable.
void main() {
int number = 7;
print('$number is even? ${number.isEven}');
print('$number is odd? ${number.isOdd}');
}
Output
7 is even? false
7 is odd? true
Explanation:
n.isEven:
Returnstrue
if the number is even.n.isOdd:
Returnstrue
if the number is odd.- Clean, readable, and more idiomatic Dart code.
Summary
Determining whether a number is even or odd in Dart is straightforward, thanks to simple arithmetic and built-in features like isEven and isOdd. This concept forms the foundation for many programming tasks, from basic logic to complex algorithms.
Thanks