# Average of first n natural numbers

Here given code implementation process.

```
// C Program
// Average of first n natural numbers
#include <stdio.h>
void findAverage(int n)
{
if (n <= 0)
{
return;
}
// Calculate sum of n natural number
// Formula = (n × (n + 1)) / 2.0
double sum = (n * (n + 1)) / 2.0;
// Calculate Average
double average = sum / n;
// Display calculated result
printf("\n %lf", average);
}
int main()
{
int n = 6;
/*
n = 6
---------------------------
1 + 2 + 3 + 4 + 5 + 6 = (21)
----------------------------
21
-- = 3.5
6
*/
findAverage(n);
return 0;
}
```

#### Output

` 3.500000`

```
// Java program for
// Average of first n natural numbers
public class Average
{
public void findAverage(int n)
{
if (n <= 0)
{
return;
}
// Calculate sum of n natural number
// Formula = (n × (n + 1)) / 2.0
double sum = (n * (n + 1)) / 2.0;
// Calculate Average
double average = sum / n;
// Display calculated result
System.out.print("\n " + average);
}
public static void main(String[] args)
{
Average task = new Average();
int n = 6;
/*
n = 6
---------------------------
1 + 2 + 3 + 4 + 5 + 6 = (21)
----------------------------
21
-- = 3.5
6
*/
task.findAverage(n);
}
}
```

#### Output

` 3.5`

```
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Average of first n natural numbers
class Average
{
public: void findAverage(int n)
{
if (n <= 0)
{
return;
}
// Calculate sum of n natural number
// Formula = (n × (n + 1)) / 2.0
double sum = (n *(n + 1)) / 2.0;
// Calculate Average
double average = sum / n;
// Display calculated result
cout << "\n " << average;
}
};
int main()
{
Average *task = new Average();
int n = 6;
/*
n = 6
---------------------------
1 + 2 + 3 + 4 + 5 + 6 = (21)
----------------------------
21
-- = 3.5
6
*/
task->findAverage(n);
return 0;
}
```

#### Output

` 3.5`

```
// Include namespace system
using System;
// Csharp program for
// Average of first n natural numbers
public class Average
{
public void findAverage(int n)
{
if (n <= 0)
{
return;
}
// Calculate sum of n natural number
// Formula = (n × (n + 1)) / 2.0
double sum = (n * (n + 1)) / 2.0;
// Calculate Average
double average = sum / n;
// Display calculated result
Console.Write("\n " + average);
}
public static void Main(String[] args)
{
Average task = new Average();
int n = 6;
/*
n = 6
---------------------------
1 + 2 + 3 + 4 + 5 + 6 = (21)
----------------------------
21
-- = 3.5
6
*/
task.findAverage(n);
}
}
```

#### Output

` 3.5`

```
package main
import "fmt"
// Go program for
// Average of first n natural numbers
type Average struct {}
func getAverage() * Average {
var me *Average = &Average {}
return me
}
func(this Average) findAverage(n int) {
if n <= 0 {
return
}
// Calculate sum of n natural number
// Formula = (n × (n + 1)) / 2.0
var sum float64 = float64(n * (n + 1)) / 2.0
// Calculate Average
var average float64 = sum / float64(n)
// Display calculated result
fmt.Print("\n ", average)
}
func main() {
var task * Average = getAverage()
var n int = 6
/*
n = 6
---------------------------
1 + 2 + 3 + 4 + 5 + 6 = (21)
----------------------------
21
-- = 3.5
6
*/
task.findAverage(n)
}
```

#### Output

` 3.5`

```
<?php
// Php program for
// Average of first n natural numbers
class Average
{
public function findAverage($n)
{
if ($n <= 0)
{
return;
}
// Calculate sum of n natural number
// Formula = (n × (n + 1)) / 2.0
$sum = ($n * ($n + 1)) / 2.0;
// Calculate Average
$average = $sum / $n;
// Display calculated result
echo("\n ".$average);
}
}
function main()
{
$task = new Average();
$n = 6;
/*
n = 6
---------------------------
1 + 2 + 3 + 4 + 5 + 6 = (21)
----------------------------
21
-- = 3.5
6
*/
$task->findAverage($n);
}
main();
```

#### Output

` 3.5`

```
// Node JS program for
// Average of first n natural numbers
class Average
{
findAverage(n)
{
if (n <= 0)
{
return;
}
// Calculate sum of n natural number
// Formula = (n × (n + 1)) / 2.0
var sum = (n * (n + 1)) / 2.0;
// Calculate Average
var average = sum / n;
// Display calculated result
process.stdout.write("\n " + average);
}
}
function main()
{
var task = new Average();
var n = 6;
/*
n = 6
---------------------------
1 + 2 + 3 + 4 + 5 + 6 = (21)
----------------------------
21
-- = 3.5
6
*/
task.findAverage(n);
}
main();
```

#### Output

` 3.5`

```
# Python 3 program for
# Average of first n natural numbers
class Average :
def findAverage(self, n) :
if (n <= 0) :
return
# Calculate sum of n natural number
# Formula = (n × (n + 1)) / 2.0
sum = (n * (n + 1)) / 2.0
# Calculate Average
average = sum / n
# Display calculated result
print("\n ", average, end = "")
def main() :
task = Average()
n = 6
# n = 6
# ---------------------------
# 1 + 2 + 3 + 4 + 5 + 6 = (21)
# ----------------------------
# 21
# -- = 3.5
# 6
task.findAverage(n)
if __name__ == "__main__": main()
```

#### Output

` 3.5`

```
# Ruby program for
# Average of first n natural numbers
class Average
def findAverage(n)
if (n <= 0)
return
end
# Calculate sum of n natural number
# Formula = (n × (n + 1)) / 2.0
sum = (n * (n + 1)) / 2.0
# Calculate Average
average = sum / n
# Display calculated result
print("\n ", average)
end
end
def main()
task = Average.new()
n = 6
# n = 6
# ---------------------------
# 1 + 2 + 3 + 4 + 5 + 6 = (21)
# ----------------------------
# 21
# -- = 3.5
# 6
task.findAverage(n)
end
main()
```

#### Output

` 3.5`

```
// Scala program for
// Average of first n natural numbers
class Average()
{
def findAverage(n: Int): Unit = {
if (n <= 0)
{
return;
}
// Calculate sum of n natural number
// Formula = (n × (n + 1)) / 2.0
var sum: Double = (n * (n + 1)) / 2.0;
// Calculate Average
var average: Double = sum / n;
// Display calculated result
print("\n " + average);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Average = new Average();
var n: Int = 6;
/*
n = 6
---------------------------
1 + 2 + 3 + 4 + 5 + 6 = (21)
----------------------------
21
-- = 3.5
6
*/
task.findAverage(n);
}
}
```

#### Output

` 3.5`

```
// Swift 4 program for
// Average of first n natural numbers
class Average
{
func findAverage(_ n: Int)
{
if (n <= 0)
{
return;
}
// Calculate sum of n natural number
// Formula = (n × (n + 1)) / 2.0
let sum: Double = Double(n * (n + 1)) / 2.0;
// Calculate Average
let average: Double = sum / Double(n);
// Display calculated result
print("\n ", average, terminator: "");
}
}
func main()
{
let task: Average = Average();
let n: Int = 6;
/*
n = 6
---------------------------
1 + 2 + 3 + 4 + 5 + 6 = (21)
----------------------------
21
-- = 3.5
6
*/
task.findAverage(n);
}
main();
```

#### Output

` 3.5`

```
// Kotlin program for
// Average of first n natural numbers
class Average
{
fun findAverage(n: Int): Unit
{
if (n <= 0)
{
return;
}
// Calculate sum of n natural number
// Formula = (n × (n + 1)) / 2.0
val sum: Double = (n * (n + 1)) / 2.0;
// Calculate Average
val average: Double = sum / n;
// Display calculated result
print("\n " + average);
}
}
fun main(args: Array < String > ): Unit
{
val task: Average = Average();
val n: Int = 6;
/*
n = 6
---------------------------
1 + 2 + 3 + 4 + 5 + 6 = (21)
----------------------------
21
-- = 3.5
6
*/
task.findAverage(n);
}
```

#### Output

` 3.5`

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

## New Comment