Skip to content
Related Articles

Related Articles

Improve Article

Find the average of first N natural numbers

  • Difficulty Level : Basic
  • Last Updated : 19 Mar, 2021

Write a program to find the Average of first N natural number. 
Examples: 
 

Input  : 10
Output : 5.5
1+2+3+4+5+6+7+8+9+10 = 5.5

Input  : 7
Output : 4.0
1+2+3+4+5+6+7 = 4

 

Prerequisite : Sum of first n natural numbers.
As discussed in previous post, sum of n natural number n(n+1)/2, we find the Average of n natural number so divide by n is n(n+1)/2*n = (n+1)/2. Here 1 if first term and n is last term. 
 

C++




// CPP Program to find the Average of first
// n natural numbers
#include <bits/stdc++.h>
using namespace std;
 
// Return the average of first n natural numbers
float avgOfFirstN(int n)
{
    return (float)(1 + n)/2;
}
 
// Driven Program
int main()
{
    int n = 20;
    cout << avgOfFirstN(n) << endl;
    return 0;
}

Java




// Java Program to find the Average of first
// n natural numbers
import java.io.*;
 
class GFG {
 
    // Return the average of first n
    // natural numbers
    static float avgOfFirstN(int n)
    {
        return (float)(1 + n) / 2;
    }
 
    // Driven Program
    public static void main(String args[])
    {
        int n = 20;
        System.out.println(avgOfFirstN(n));
    }
}
 
/*This code is contributed by Nikita tiwari.*/

Python3




# Python 3 Program to find the Average
# of first n natural numbers
 
# Return the average of first n
# natural numbers
def avgOfFirstN(n) :
    return (float)(1 + n) / 2;
 
# Driven Program
n = 20
print(avgOfFirstN(n))
 
# This code is contributed by Nikita Tiwari.

C#




// C#Program to find the Average of first
// n natural numbers
using System;
 
class GFG {
 
    // Return the average of first n
    // natural numbers
    static float avgOfFirstN(int n)
    {
        return (float)(1 + n) / 2;
    }
 
    // Driven Program
    public static void Main()
    {
        int n = 20;
        Console.WriteLine(avgOfFirstN(n));
    }
}
 
/*This code is contributed by vt_m.*/

PHP




<?php
// PHP Program to find
// the Average of first
// n natural numbers
 
// Return the average
// of first n natural
// numbers
function avgOfFirstN($n)
{
    return (float)(1 + $n) / 2;
}
 
// Driver Code
$n = 20;
echo(avgOfFirstN($n));
 
// This code is contributed by Ajit.
?>

Javascript




<script>
 
// javascript Program to find the Average of first
// n natural numbers
 
// Return the average of first n natural numbers
function avgOfFirstN( n)
{
    return (1 + n)/2;
}
 
// Driven Program
 
    let n = 20;
    document.write(avgOfFirstN(n));
 
// This code is contributed by todaysgaurav
 
</script>

Output: 

10.5

Time Complexity : O(1) 
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :