Average of odd numbers till a given odd number
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++
// Program to find average of odd numbers // till a given odd number. #include <stdio.h> // Function to calculate the average // of odd numbers int 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 function int main() { int n = 15; printf("%d", averageOdd(n)); return 0; } |
Java
// 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.*/ |
Python3
# Program to find average # of odd numbers till a # given odd number. # Function to calculate # the average of odd # numbers def 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 function n = 15print(averageOdd(n)) # This code is contributed by Nikita Tiwari. |
C#
// 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
<?php // 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) { 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. ?> |
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
// Program to find average of odd numbers // till a given odd number. #include <stdio.h> // Function to calculate the average // of odd numbers int averageOdd(int n) { if (n % 2 == 0) { printf("Invalid Input"); return -1; } return (n + 1) / 2; } // driver function int main() { int n = 15; printf("%d", averageOdd(n)); return 0; } |
Java
// 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. |
Python3
# Program to find average of odd # numbers till a given odd number. # Function to calculate the # average of odd numbers def averageOdd(n) : if (n % 2 == 0) : print("Invalid Input") return -1 return (n + 1) // 2 # driver function n = 15print(averageOdd(n)) # This code is contributed by Nikita tiwari. |
C#
// 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
<?php // 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) { echo("Invalid Input"); return -1; } return ($n + 1) / 2; } // Driver Code $n = 15; echo(averageOdd($n)); // This code is contributed by vt_m. ?> |
Output:
8
Recommended Posts:
- Average of even numbers till a given even number
- Least prime factor of numbers till n
- Program to print triangular number series till n
- Average of first n even natural numbers
- Average of a stream of numbers
- Average of first n odd naturals numbers
- Compute average of two numbers without overflow
- Average of Squares of Natural Numbers
- Find the average of first N natural numbers
- Find average of two numbers using bit operation
- Sum of the series 1, 2, 4, 3, 5, 7, 9, 6, 8, 10, 11, 13.. till N-th term
- Alternate Primes till N
- Find the average of k digits from the beginning and l digits from the end of the given number
- Sum of Digits in a^n till a single digit
- Represent a number as a sum of maximum possible number of Prime Numbers
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.
Improved By : vt_m



