Find the product of digits at odd/even places The 'score' of a race is given.

We have to calculate the 'bonus points' to be given to the participant.

The 'bonus points' are dependent on the relation between product of digits at even places and product of digits at odd places.

So essentially I have to find the product of digits at even and odd places.

I first counted the number of digits in 'score' and then went on storing the product of digits in odd/even places in separate integers reducing one digit at a time

Code in Java

import java.util.Scanner;

class BonusPoints {
    public static int countDigits(int dist) {
        int counter=0;
        while (dist&gt;0) {
            dist /= 10;
            counter++;
        }
        return counter;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int dist = sc.nextInt();
        int bonus=0;
        int prodOdd = 1;
        int prodEven = 1;
        int currDigit = 0;

        if (dist &lt; 0) {
            System.out.printf("Invalid Input ");
            System.exit(0);
        }

        int counter = countDigits(dist);
        int lol = counter;
        //System.out.println("Number of Digits are " + counter);

        for (int i = 0; i &lt; lol; i++) {

            //System.out.println("Current Dist " + dist);

            currDigit = dist % 10;
            //System.out.println("Current Digit " + currDigit);

            if (counter % 2 == 0){
                prodEven *= currDigit;
                //System.out.println("Current ProdEven "+prodEven);
            }
            else {
                prodOdd *= currDigit;
                //System.out.println("Current ProdOdd " +prodEven);
            }

            dist /= 10;
            counter--;
        }

        if (prodOdd == prodEven)
            bonus = 2 * prodOdd;
        else
            bonus = prodOdd &gt; prodEven ? prodOdd : prodEven;

        System.out.println("The bonus is "+bonus);
    }
}


Those comments were used for debugging process while writing the code

Is there a better method to do this and what are changes that one can make to this code to optimize it?
Program structure

Separate I/O from the calculation and keep the main function short:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int dist = sc.nextInt();
    if (dist &lt; 0) {
        // ... report error and terminate  program.

    }

    int bonus = calculateBonus(dist);
    System.out.println("The bonus is " + bonus);
}


That makes the program structure clearer, makes the functions reusable, and allows to add test cases easily.

Handle invalid input

By convention, error messages are written to the standard error output, and a nonzero exit code indicates an abnormal program termination. Instead of "Invalid Input" I suggest to print a message indicating the correct usage:

if (dist &lt; 0) {
    System.err.printf("Input must be a non-negative integer.");
    System.exit(1);
}


Miscellaneous

Inconsistent white space here:

int bonus=0;
int prodOdd = 1;


It can be difficult to choose a good variable name, but I am sure that you'll find something better here:

int lol = counter;


Declare variables at the nearest scope where they are used. As an example, currDigit is only used inside the for-loop:

for (int i = 0; i &lt; lol; i++) {
    int currDigit = dist % 10;
    // ...
}


Simplifiying the program

The calculation of

    if (prodOdd == prodEven)
        bonus = 2 * prodOdd;
    else
        bonus = prodOdd &gt; prodEven ? prodOdd : prodEven;


does not change if we exchange the even and the odd product. Therefore it does not matter if we count even and odd position from the most significant (decimal) digit or from the least significant digit.

As a consequence, we can process the digits starting with the least significant one, and it is not necessary to calculate the number of digits beforehand.

Instead of the counter, a boolean variable is sufficient to keep track of even and odd positions. 

So the countDigits() function is obsolete, and calculateBonus() could be implemented like this:

public static int calculateBonus(int dist) {
    int prodOdd = 1;
    int prodEven = 1;
    boolean evenPosition = true;

    while (dist &gt; 0) {
        int currDigit = dist % 10;
        if (evenPosition) {
            prodEven *= currDigit;
        } else {
            prodOdd *= currDigit;
        }

        dist /= 10;
        evenPosition = !evenPosition;
    }

    return prodOdd == prodEven ?
        2 * prodOdd :
        prodOdd &gt; prodEven ? prodOdd : prodEven;
}