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
#include <stdio.h>
int averageOdd( int n)
{
if (n % 2 == 0) {
printf ( "Invalid Input" );
return -1;
}
int sum = 0, count = 0;
while (n >= 1) {
count++;
sum += n;
n = n - 2;
}
return sum / count;
}
int main()
{
int n = 15;
printf ( "%d" , averageOdd(n));
return 0;
}
|
Java
import java.io.*;
class GFG {
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++;
sum += n;
n = n - 2 ;
}
return sum / count;
}
public static void main(String args[])
{
int n = 15 ;
System.out.println(averageOdd(n));
}
}
|
Python3
def averageOdd(n):
if (n % 2 = = 0 ):
print ( "Invalid Input" )
return - 1
sm = 0
count = 0
while (n > = 1 ):
count = count + 1
sm = sm + n
n = n - 2
return sm / / count
n = 15
print (averageOdd(n))
|
C#
using System;
class GFG {
static int averageOdd( int n)
{
if (n % 2 == 0) {
Console.Write( "Invalid Input" );
return -1;
}
int sum = 0, count = 0;
while (n >= 1) {
count++;
sum += n;
n = n - 2;
}
return sum / count;
}
public static void Main()
{
int n = 15;
Console.Write(averageOdd(n));
}
}
|
PHP
<?php
function averageOdd( $n )
{
if ( $n % 2 == 0)
{
echo ( "Invalid Input" );
return -1;
}
$sum = 0;
$count = 0;
while ( $n >= 1)
{
$count ++;
$sum += $n ;
$n = $n - 2;
}
return $sum / $count ;
}
$n = 15;
echo (averageOdd( $n ));
?>
|
Javascript
<script>
function averageOdd( n)
{
if (n % 2 == 0)
{
document.write( "Invalid Input" );
return -1;
}
let sum = 0, count = 0;
while (n >= 1) {
count++;
sum += n;
n = n - 2;
}
return sum / count;
}
let n = 15;
document.write(averageOdd(n));
</script>
|
C++
#include <iostream>
using namespace std;
int averageOdd( int n)
{
if (n % 2 == 0) {
cout << "Invalid Input" ;
return -1;
}
int sum = 0, count = 0;
while (n >= 1) {
count++;
sum += n;
n = n - 2;
}
return sum / count;
}
int main()
{
int n = 15;
cout << " " << averageOdd(n);
return 0;
}
|
Output:
8
Time Complexity: O(n) //since there runs a loop from 0 to (n – 1).
Auxiliary Space: O(1) // since no extra array or data structure is used so the space taken by the algorithm is constant
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++
#include <bits/stdc++.h>
using namespace std;
int averageOdd( int n)
{
if (n % 2 == 0) {
cout << "Invalid Input" << endl;
return -1;
}
return (n + 1) / 2;
}
int main()
{
int n = 15;
cout << averageOdd(n) << endl;
return 0;
}
|
C
#include <stdio.h>
int averageOdd( int n)
{
if (n % 2 == 0) {
printf ( "Invalid Input" );
return -1;
}
return (n + 1) / 2;
}
int main()
{
int n = 15;
printf ( "%d" , averageOdd(n));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int averageOdd( int n)
{
if (n % 2 == 0 )
{
System.out.println( "Invalid Input" );
return - 1 ;
}
return (n + 1 ) / 2 ;
}
public static void main(String args[])
{
int n = 15 ;
System.out.println(averageOdd(n));
}
}
|
Python3
def averageOdd(n) :
if (n % 2 = = 0 ) :
print ( "Invalid Input" )
return - 1
return (n + 1 ) / / 2
n = 15
print (averageOdd(n))
|
C#
using System;
class GFG
{
static int averageOdd( int n)
{
if (n % 2 == 0)
{
Console.Write( "Invalid Input" );
return -1;
}
return (n + 1) / 2;
}
public static void Main()
{
int n = 15;
Console.Write(averageOdd(n));
}
}
|
PHP
<?php
function averageOdd( $n )
{
if ( $n % 2 == 0)
{
echo ( "Invalid Input" );
return -1;
}
return ( $n + 1) / 2;
}
$n = 15;
echo (averageOdd( $n ));
?>
|
Javascript
<script>
function averageOdd( n)
{
if (n % 2 == 0) {
document.write( "Invalid Input" );
return -1;
}
return (n + 1) / 2;
}
let n = 15;
document.write( averageOdd(n));
</script>
|
Output:
8
Time complexity: O(1) since performing constant operations
Space Complexity: O(1) since using constant variables
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...