Find the average of first N natural numbers
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; } |
chevron_right
filter_none
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.*/ |
chevron_right
filter_none
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 = 20print(avgOfFirstN(n)) # This code is contributed by Nikita Tiwari. |
chevron_right
filter_none
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.*/ |
chevron_right
filter_none
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. ?> |
chevron_right
filter_none
Output:
10.5
Time Complexity : O(1)
Recommended Posts:
- Average of first n even natural numbers
- Average of Squares of Natural Numbers
- Find average of two numbers using bit operation
- Find if given number is sum of first n natural numbers
- Program to find sum of first n natural numbers
- Find m-th summation of first n natural numbers.
- Find sum of N-th group of Natural Numbers
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Find the good permutation of first N natural numbers
- Find permutation of first N natural numbers that satisfies the given condition
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Average numbers in array
- Average of first n odd naturals numbers
- Average of a stream of 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 : jit_t



