Average of odd numbers till a given odd number - GeeksforGeeks

24 Mar, 2021
Given an odd number n, find the average of odd numbers from 1 to n.Examples:  
Input : n = 9
Output : 5
Explanation
(1 + 3 + 5 + 7 + 9)/5 
= 25/5 
= 5

Input : n = 221
Output : 111
 
Method 1 We can calculate average by adding each odd numbers till n and then dividing sum by count.Below is the implementation of the approach.  
C++
Java
Python3
C#
PHP
Javascript
// Program to find average of odd numbers// till a given odd number.#include <stdio.h> // Function to calculate the average// of odd numbersint averageOdd(int n){    if (n % 2 == 0) {        printf("Invalid Input");        return -1;    }     int sum = 0, count = 0;    while (n >= 1) {         // count odd numbers        count++;         // store the sum of odd numbers        sum += n;         n = n - 2;    }    return sum / count;} // driver functionint main(){    int n = 15;    printf("%d", averageOdd(n));    return 0;}
// Program to find average of odd numbers// till a given odd number.import java.io.*; class GFG {         // Function to calculate the average    // of odd numbers    static int averageOdd(int n)    {        if (n % 2 == 0) {            System.out.println("Invalid Input");            return -1;        }              int sum = 0, count = 0;        while (n >= 1) {                  // count odd numbers            count++;                  // store the sum of odd numbers            sum += n;                  n = n - 2;        }        return sum / count;    }          // driver function    public static void main(String args[])    {        int n = 15;        System.out.println(averageOdd(n));    }} /*This code is contributed by Nikita tiwari.*/
# Program to find average# of odd numbers till a# given odd number. # Function to calculate# the average of odd# numbersdef averageOdd(n) :     if (n % 2 == 0) :        print("Invalid Input")        return -1         sm = 0    count = 0     while (n >= 1) :         # count odd numbers        count = count + 1         # store the sum of        # odd numbers        sm = sm + n         n = n - 2         return sm // count     # Driver functionn = 15print(averageOdd(n)) # This code is contributed by Nikita Tiwari.
// C# Program to find average // of odd numbers till a given// odd number.using System; class GFG {         // Function to calculate the    // average of odd numbers    static int averageOdd(int n)    {        if (n % 2 == 0) {            Console.Write("Invalid Input");            return -1;        }             int sum = 0, count = 0;        while (n >= 1) {                 // count odd numbers            count++;                 // store the sum of odd numbers            sum += n;                 n = n - 2;        }        return sum / count;    }         // driver function    public static void Main()    {        int n = 15;        Console.Write(averageOdd(n));    }} /*This code is contributed by vt_m.*/
<?php// Program to find average of odd// numbers till a given odd number. // Function to calculate the// average of odd numbersfunction averageOdd($n){    if ($n % 2 == 0)    {        echo("Invalid Input");        return -1;    }     $sum = 0;    $count = 0;    while ($n >= 1)    {         // count odd numbers        $count++;         // store the sum of        // odd numbers        $sum += $n;         $n = $n - 2;    }    return $sum / $count;}     // Driver Code    $n = 15;    echo(averageOdd($n));     // This code is contributed by vt_m.?>
<script>// Javascript Program to find average of odd numbers// till a given odd number.     // Function to calculate the average    // of odd numbers    function averageOdd( n)    {        if (n % 2 == 0)        {            document.write("Invalid Input");            return -1;        }         let sum = 0, count = 0;        while (n >= 1) {             // count odd numbers            count++;             // store the sum of odd numbers            sum += n;            n = n - 2;        }        return sum / count;    }     // driver function    let n = 15;    document.write(averageOdd(n)); // This code is contributed by gauravrajput1</script>
Output:  
8
Method 2 The average of odd numbers can find out only in single steps by using the following formula [n + 1 ] / 2 where n is last odd number.How does this formula work?  
We know there are (n+1)/2 odd numbers till n.  

For example:
There are two odd numbers till 3 and there are
three odd numbers till 5.

Sum of first k odd numbers is k*k 

Sum of odd numbers till n is ((n+1)/2)2

Average of odd numbers till n is (n + 1)/2
Below is the implementation of the approach.  
C
Java
Python3
C#
PHP
Javascript
// Program to find average of odd numbers// till a given odd number.#include <stdio.h> // Function to calculate the average// of odd numbersint averageOdd(int n){    if (n % 2 == 0) {        printf("Invalid Input");        return -1;    }     return (n + 1) / 2;} // driver functionint main(){    int n = 15;    printf("%d", averageOdd(n));    return 0;}
// Program to find average of odd// numbers till a given odd number.import java.io.*; class GFG{    // Function to calculate the    // average of odd numbers    static int averageOdd(int n)    {        if (n % 2 == 0)        {            System.out.println("Invalid Input");            return -1;        }             return (n + 1) / 2;    }         // driver function    public static void main(String args[])    {        int n = 15;        System.out.println(averageOdd(n));    }} // This code is contributed by Nikita tiwari.
# Program to find average of odd# numbers till a given odd number. # Function to calculate the# average of odd numbersdef averageOdd(n) :    if (n % 2 == 0) :        print("Invalid Input")        return -1              return (n + 1) // 2     # driver functionn = 15print(averageOdd(n))   # This code is contributed by Nikita tiwari.
// C# Program to find average// of odd numbers till a given// odd number.using System; class GFG{    // Function to calculate the    // average of odd numbers    static int averageOdd(int n)    {        if (n % 2 == 0)        {            Console.Write("Invalid Input");            return -1;        }             return (n + 1) / 2;    }         // driver function    public static void Main()    {        int n = 15;        Console.Write(averageOdd(n));    }} // This code is contributed by vt_m.
<?php// Program to find average of odd// numbers till a given odd number. // Function to calculate the// average of odd numbersfunction averageOdd( $n){    if ($n % 2 == 0)    {        echo("Invalid Input");        return -1;    }     return ($n + 1) / 2;}     // Driver Code    $n = 15;    echo(averageOdd($n));     // This code is contributed by vt_m.?>
<script> // javascript Program to find average of odd numbers// till a given odd number. // Function to calculate the average// of odd numbersfunction averageOdd( n){    if (n % 2 == 0) {        document.write("Invalid Input");        return -1;    }     return (n + 1) / 2;} // driver function    let n = 15;    document.write( averageOdd(n));     // This code is contributed by todaysgaurav </script>
Output: 
8
 
vt_m
todaysgaurav
GauravRajput1
number-theory
series
series-sum
Mathematical
number-theory
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Print all possible combinations of r elements in a given array of size n
Program for factorial of a number
Operators in C / C++
The Knight's tour problem | Backtracking-1
Find minimum number of coins that make a given value
Program for Decimal to Binary Conversion
Program to find sum of elements in a given array