Find the Batting Average of a batsman

05 Apr, 2021
Given three integers runs, matches, and not-out 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. 
where  
Note: If the batsman was never dismissed, print “NA” as the no average can be defined.
Examples:  
Input: runs = 10000, matches = 250, not-out = 50 Output: 50 Explanation: Number of times batsman was dismissed = 250 – 50 = 200 Batting Average = 10000 / 200 = 50.
Input: runs = 100, matches = 1, not-out = 1 Output: NA  
Approach: Follow the steps below to solve the problem: 
Calculate the number of dismissals, equal to matches – notout.Calculate the Batting Average, equal to runs/ (matches – notout).
Calculate the number of dismissals, equal to matches – notout.
Calculate the Batting Average, equal to runs/ (matches – notout).
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to calculate// the average of a batsman#include <bits/stdc++.h>using namespace std; // Function to find the average// of a batsmandouble 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 Programint 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;}
// Java program to calculate// the average of a batsmanclass GFG{     // Function to find the average// of a batsmanstatic 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 codepublic 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
# Python3 program to calculate# the average of a batsman # Function to find the average# of a batsmandef 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 Programruns = 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
// C# program to calculate// the average of a batsmanusing System;class GFG{      // Function to find the average// of a batsmanstatic 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 codepublic 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
<script> // Javascript program to calculate// the average of a batsman // Function to find the average// of a batsmanfunction averageRuns(runs, matches, notout){         // Calculate number of    // dismissals    let out1;    out1 = matches - notout;     // Check for 0 times out    if (out1 == 0)        return -1;     // Calculate batting average    let avg = parseInt((runs) / out1, 10);     return avg;} // Driver code     let runs = 10000;let matches = 250;let notout = 50;let avg = averageRuns(runs, matches, notout); if (avg == -1)    document.write("NA");else    document.write(avg); // This code is contributed by divyesh072019 </script>
50
 
Time Complexity: O(1) Auxiliary Space: O(1) 
bansal_rtk_
rock_cool
Akanksha_Rai
divyesh072019
khushboogoyal499
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java