Bonus Points – Bike Race Program in java

Question:

There is an app for bike race which provides bonus points for the player.  In this app, the player has to play the race and on completion, the total kilometers traveled by the player is calculated. Based on this distance traveled, the product of digits in the odd position and also a product of digits in the even position is calculated.  Whichever is highest, that is the bonus points given to the user.   If the product of odd and even position digits are the same, then the player should receive double the product as a bonus.

Example : If the distance travelled  is 5632 
Product of digits in odd position = 5 * 3 = 15
Product of digits in even  position = 6 * 2 = 12
As 15 > 12, the bonus points the player gets is 15.

Write a program  to do this operation.

Create a class BikeRace.java with the main method.

Note : Input should be the distance travelled and the output is the bonus points.  If the input is less than zero, the output should be “Invalid Input”.  

Sample Input 1 :

Enter the distance travelled

8694

Sample Output 1 :

Your bonus points is 72

Sample Input 2 :

Enter the distance travelled

263

Sample Output 2 :

Your bonus points is 12

CODE:

import java.util.Scanner;


     public class BikeRace{
         public static void main (String[] args) {
             int r,pdeven=1,pdodd=1;
             Scanner sc = new Scanner(System.in);
             System.out.println("Enter the distance travelled");
             int dist = sc.nextInt();
            if(dist<0)
             {
                System.out.println("Invalid Input");
            }
            else if(dist==0){
                System.out.println("Your bonus points is 0");
            }
            else{
                
                while(dist>0)
                {
                 r= dist%10;
                 pdeven*=r;
                 dist/=10;
                 if(dist>0){
                     r=dist%10;
                     pdodd*=r;
                     dist/=10;
                 }
                }
                if(pdeven==pdodd){
                    System.out.println("Your bonus points is "+(pdeven*2));
                }
                else if(pdeven>pdodd){
                    System.out.println("Your bonus points is "+(pdeven));
                }
                else{
                    System.out.println("Your bonus points is "+(pdodd));
                }
            }
        }
   

Leave a Comment

Scroll to Top