Count of odd numbers between low and high in Java

Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).

    public int countOdds(int low, int high) { 

        int oddNumbers = 0; 

        // Find nearest odd number of inputs 

        low = (low % 2 == 0) ? low + 1 : low; 

        high = (high % 2 == 0) ? high - 1 : high; 

        for (int i = low; i <= high; i+=2) { 

            oddNumbers++; 

        

        return oddNumbers; 

    }

Comments

Popular posts from this blog

Unsupervised Learning

Setting up Python Flask server on internet via Port forwarding

Automate Blog Post creation using Blogger APIs and Python

The beginning of Data Quest

Setting up Jupyter Lab integrated with Python, Julia, R on Windows Subsystem for Linux (Ubuntu)