Program to find the head start in a race in C++


In this problem, we are given two integers that give the head start that is given by A to B and C respectively in a 100-meter race. Our task is to create a program to find the head start in a race in C++.

Code Description − Here, there are head starts that are given by A to B and A to C respectively in a 100-meter race. We need to find the relative head start that is given by B to C in the 100-meter race.

Let’s take an example to understand the problem,

Input

20, 28

Output

90

Explanation

A gives B a head-start of 15 i.e. if A completes 100 m, then B completes 80.

A gives C a head-start of 28 i.e. if A completes 100 m, then B completes 72.

Now, when B completes 80 meters, C completes 72.

If B completes 100, C completed

(72 * 100) / 80 = 90

Solution Approach

To solve the problem, find the distance that B and C complete when A completes 100 meters. After this we calculate the distance that C completes when B completes 100, using the formula −

(C * 100) B

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
int CalcHeadStart(int B, int C) {
   return ( 100 - ( ( (100 - C)*100 ) / (100 - B)) ) ;
}
int main() {
   int B = 12, C = 34;
   cout<<"Head start in a race by B to C is "<<CalcHeadStart(B, C) << " meters";
   return 0;
}

Output

Head start in a race by B to C is 25 meters

Updated on: 15-Sep-2020

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements