Home > Net >  Why in console even numbers gives empty list?
Why in console even numbers gives empty list?

Time:03-03

void  main() {
    List<int> numbers = [55,58,62,15,14,19,20]; 
    List oddNumbers = []; 
    List evenNumbers = [];

    for (int index = 0; index < numbers.length; index  ) {
        print(numbers[index]);
        if(numbers[index]% 2 !=0) {
            oddNumbers.add(numbers[index]);
        } else if(numbers[index]%2 != 0) {
            evenNumbers.add(numbers[index]);
        }
    }
    print("Odd numbers:$oddNumbers");
    print("Even numbers:$evenNumbers");
}

it gives:

Odd numbers:[55, 15, 19]
Even numbers:[] 

Why the even numbers list is empty?

CodePudding user response:

Hi You're using same logic in both if and else conditions. Please check below snippet.

if(numbers[index]% 2 !=0) { 
oddNumbers.add(numbers[index]); } else 
if(numbers[index]%2 == 0) { 
evenNumbers.add(numbers[index]); } }
  •  Tags:  
  • dart
  • Related