Find the Batting Average of a batsman

Given three integers runs, matches, and notout representing the number of runs scored, number of innings played by the batsman and number of times he remained Not Out respectively, the task is to calculate the Batting Average of the batsman.

\text{Batting Average} = \frac{\text{Runs Scored}}{\text{Number of dismissals}}
where
\text{Number of dismissals = Number of innings - Number of innings he remained Not Out}

Note: If the batsman was never dismissed, print “NA” as the no average can be defined.

Examples:

Input: runs = 10000, matches = 250, notout = 50
Output: 50
Explaination:
Number of times batsman was dismissed = 250 – 50 = 200
Batting Average = 10000 / 200 = 50.



Input: runs = 100, matches = 1, notout = 1
Output: NA

Approach:
Follow the steps below to solve the problem:

  1. Calculate the number of dismissals, equal to matches – notout.
  2. Calculate the Batting Average, equal to runs/ (matches – notout).

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to calculate
// the average of a batsman
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the average
// of a batsman
double averageRuns(int runs,
                   int matches,
                   int notout)
{
    // Calculate number of
    // dismissals
    int out = matches - notout;
  
    // check for 0 times out
    if (out == 0)
        return -1;
  
    // Calculate batting average
    double avg = double(runs) / out;
  
    return avg;
}
  
// Driver Program
int main()
{
    int runs = 10000;
    int matches = 250;
    int notout = 50;
  
    double avg
        = averageRuns(
            runs, matches, notout);
  
    if (avg == -1)
        cout << "NA";
    else
        cout << avg;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to calculate 
// the average of a batsman 
class GFG{ 
      
// Function to find the average 
// of a batsman 
static int averageRuns(int runs, 
                       int matches, 
                       int notout) 
      
    // Calculate number of 
    // dismissals 
    int out = matches - notout; 
  
    // Check for 0 times out 
    if (out == 0
        return -1
  
    // Calculate batting average 
    int avg = (runs) / out; 
  
    return avg; 
  
// Driver code 
public static void main(String[] args) 
    int runs = 10000
    int matches = 250
    int notout = 50
  
    int avg = averageRuns(runs, matches, 
                                notout); 
    if (avg == -1
        System.out.print("NA"); 
    else
        System.out.print(avg); 
}
}
  
// This code is contributed by Ritik Bansal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to calculate
# the average of a batsman
  
# Function to find the average
# of a batsman
def averageRuns(runs, matches, notout):
  
    # Calculate number of
    # dismissals
    out = matches - notout;
  
    # check for 0 times out
    if (out == 0):
        return -1;
  
    # Calculate batting average
    avg = runs // out;
  
    return avg;
  
# Driver Program
runs = 10000;
matches = 250;
notout = 50;
  
avg = averageRuns(runs, matches, notout);
  
if (avg == -1):
    print("NA");
else:
    print(avg);
  
# This code is contributed by Akanksha_rai

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to calculate 
// the average of a batsman
using System;
class GFG{ 
       
// Function to find the average 
// of a batsman 
static int averageRuns(int runs, 
                       int matches, 
                       int notout) 
       
    // Calculate number of 
    // dismissals 
    int out1;
    out1 = matches - notout; 
   
    // Check for 0 times out 
    if (out1 == 0) 
        return -1; 
   
    // Calculate batting average 
    int avg = (runs) / out1; 
   
    return avg; 
   
// Driver code 
public static void Main (string[] args)
    int runs = 10000; 
    int matches = 250; 
    int notout = 50; 
   
    int avg = averageRuns(runs, matches, 
                                notout); 
    if (avg == -1) 
        Console.Write("NA"); 
    else
        Console.Write(avg); 
}
}
   
// This code is contributed by rock_cool

chevron_right


Output:

50

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.