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>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 < 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 < 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 > 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?