# Program for weighted mean of natural numbers.

There are given an array of natural numbers and another array with corresponding weight of the number. Then we have to calculate the weighted mean. Where x (bar) is called the weighted mean, x[i] is the elements of array, and W[i] is the weight of elements of array x[i].

Examples –

```Input :
X[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
W[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
weighted mean
= (W * X + W * X + W * X + . . . +
W[n-1] * X[n-1]) / (W + W + W + . . . + W[n-1])
= (1 * 1 + 2 * 2 + 3 * 3 + . . . + 10 * 10) /
(1 + 2 + 3 + . . . + 10)
= 385 / 55 = 7
Output : 7

Input :
X[] = {3, 4, 5, 6, 7}
W[] = {4, 5, 6, 7, 8}
weighted mean
= (W * X + W * X + W * X + . . . +
W[n-1] * X[n-1]) / (W + W + W + . . . + W[n-1])
= (3 * 4 + 4 * 5 + 5 * 6 + 6 * 7 + 7 * 8) /
(4 + 5 + 6 + 7 + 8)
= 160 / 30 = 5.33333
Output : 5.33333
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution to solve weighed mean.

## C++

 `// Program to find weighted mean of ` `// natural numbers. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to calculate weighted mean. ` `float` `weightedMean(``int` `X[], ``int` `W[], ``int` `n) ` `{ ` `    ``int` `sum = 0, numWeight = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``numWeight = numWeight + X[i] * W[i]; ` `        ``sum = sum + W[i]; ` `    ``} ` ` `  `    ``return` `(``float``)numWeight / sum; ` `} ` ` `  `// Driver program to test the function. ` `int` `main() ` `{ ` `    ``// Take num array and corresponding weight   ` `    ``// array and initialize it. ` `    ``int` `X[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; ` `    ``int` `W[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; ` ` `  `    ``// Calculate the size of array. ` `    ``int` `n = ``sizeof``(X)/``sizeof``(X); ` `    ``int` `m = ``sizeof``(W)/``sizeof``(W); ` ` `  `    ``// Check the size of both array is equal or not. ` `    ``if` `(n == m) ` `        ``cout << weightedMean(X, W, n); ` `    ``else` `        ``cout << ``"-1"``; ` `    ``return` `0; ` `}  `

## Java

 `// JAVA Program to find weighted mean ` `// of natural numbers. ` ` `  `class` `GFG { ` `     `  `    ``// Function to calculate weighted mean. ` `    ``static` `float` `weightedMean(``int` `X[], ``int` `W[], ` `                                        ``int` `n) ` `    ``{ ` `        ``int` `sum = ``0``, numWeight = ``0``; ` `      `  `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``numWeight = numWeight + X[i] * W[i]; ` `            ``sum = sum + W[i]; ` `        ``} ` `      `  `        ``return` `(``float``)(numWeight) / sum; ` `    ``} ` `      `  `    ``// Driver program to test the function. ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``// Take num array and corresponding  ` `        ``// weight array and initialize it. ` `        ``int` `X[] = {``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``10``}; ` `        ``int` `W[] = {``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``10``}; ` `      `  `        ``// Calculate the size of array. ` `        ``int` `n = X.length; ` `        ``int` `m = W.length; ` `      `  `        ``// Check the size of both array is  ` `        ``// equal or not. ` `        ``if` `(n == m) ` `            ``System.out.println(weightedMean(X, W, n)); ` `        ``else` `            ``System.out.println(``"-1"` `); ` `         `  `    ``}  ` `} ` ` `  `/*This code is contributed by Nikita Tiwari.*/`

## Python

 `# Python Program to find weighted mean of ` `# natural numbers. ` ` `  `# Function to calculate weighted mean. ` `def` `weightedMean(X,W,n) : ` `    ``sum` `=` `0` `    ``numWeight ``=` `0` `    ``i ``=` `0` `    ``while`  `i < n : ` `         `  `        ``numWeight ``=` `numWeight ``+` `X[i] ``*` `W[i] ` `        ``sum` `=` `sum` `+` `W[i] ` `        ``i ``=` `i ``+` `1` `  `  `    ``return` `(``float``)(numWeight ``/` `sum``) ` ` `  `  `  `# Driver program to test the function. ` ` `  `# Take num array and corresponding weight   ` `# array and initialize it. ` `X ``=` `[``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``10``] ` `W ``=` `[``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``10``] ` ` `  `# Calculate the size of array. ` `n ``=` `len``(X) ` `m ``=` `len``(W) ` `  `  `# Check the size of both array is equal or not. ` `if` `(n ``=``=` `m) : ` `    ``print` `weightedMean(X, W, n) ` `else` `: ` `    ``print` `"-1"` `     `  `# This coe is contributed by Nikita Tiwari. `

## C#

 `// C# Program to find weighted mean ` `// of natural numbers. ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to calculate weighted mean. ` `    ``static` `float` `weightedMean(``int` `[]X, ``int` `[]W, ` `                                        ``int` `n) ` `    ``{ ` `        ``int` `sum = 0, numWeight = 0; ` `     `  `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``numWeight = numWeight + X[i] * W[i]; ` `            ``sum = sum + W[i]; ` `        ``} ` `     `  `        ``return` `(``float``)(numWeight) / sum; ` `    ``} ` `     `  `    ``// Driver program to test the function. ` `    ``public` `static` `void` `Main() ` `    ``{ ` `         `  `        ``// Take num array and corresponding  ` `        ``// weight array and initialize it. ` `        ``int` `[]X = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; ` `        ``int` `[]W = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; ` `     `  `        ``// Calculate the size of array. ` `        ``int` `n = X.Length; ` `        ``int` `m = W.Length; ` `     `  `        ``// Check the size of both array is  ` `        ``// equal or not. ` `        ``if` `(n == m) ` `            ``Console.WriteLine(weightedMean(X, W, n)); ` `        ``else` `            ``Console.WriteLine(``"-1"` `); ` `    ``}  ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output –

```7
```

Second method – to compute the weighted mean of first n natural numbers. It is the formula to compute the weighted mean of first n natural numbers. In this method, we have given first n natural number and their weight are also be the natural numbers. Then we generate the formula

```Weighted Mean
= (W * X + W * X + W * X + . . . +
W[n-1] * X[n-1]) / (W + W + W + . . . + W[n-1])
= (1 * 1 + 2 * 2 + 3 * 3 + . . . + n * n) / (1 + 2 + 3 + . . . + n)
= (n * (n + 1) * (2 * n + 1) / 6) / (n * (n + 1) / 2)

Weighted Mean = (2 * n + 1) / 3

Example: Weighted mean of first 10 natural numbers
n = 10
Weighted mean
= (2 * 10 + 1) / 3 = 21 / 3 = 7
```

## C++

 `// Program to find weighted mean of first ` `// n natural numbers using formula. ` `#include ` `using` `namespace` `std; ` ` `  `// Returns weighted mean assuming for numbers ` `// {1, 2, ..n} and weights {1, 2, .. n} ` `int` `weightedMean(``int` `n) ` `{ ` `    ``return` `(2 * n + 1)/3; ` `} ` ` `  `// Driver program to test the function. ` `int` `main() ` `{ ` `    ``int` `n = 10; ` `    ``cout << weightedMean(n); ` `    ``return` `0; ` `} `

## Java

 `// Program to find weighted mean of first ` `// n natural numbers using formula. ` `import` `java.io.*; ` ` `  `public` `class` `GFG { ` `     `  `    ``// Returns weighted mean assuming for numbers ` `    ``// {1, 2, ..n} and weights {1, 2, .. n} ` `    ``static` `int` `weightedMean(``int` `n) ` `    ``{ ` `        ``return` `(``2` `* n + ``1``)/``3``; ` `    ``} ` `     `  `    ``// Driver program to test the function. ` ` `  `    ``static` `public` `void` `main (String[] args) ` `    ``{ ` `        ``int` `n = ``10``; ` `         `  `        ``System.out.println(weightedMean(n)); ` `     `  `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## Python 3

 `# Program to find weighted mean of first ` `# n natural numbers using formula. ` ` `  `# Returns weighted mean assuming for numbers ` `# 1, 2, ..n and weights 1, 2, .. n ` `def` `weightedMean(n): ` ` `  `    ``return` `(``2` `*` `n ``+` `1``) ``/` `3` ` `  `# Driver program to test the function. ` `n ``=` `10` `print``(``int``(weightedMean(n))) ` `# This code is contributed by smita `

## C#

 `// Program to find weighted mean of first ` `// n natural numbers using formula. ` `using` `System; ` ` `  `public` `class` `GFG { ` `     `  `    ``// Returns weighted mean assuming for numbers ` `    ``// {1, 2, ..n} and weights {1, 2, .. n} ` `    ``static` `int` `weightedMean(``int` `n) ` `    ``{ ` `        ``return` `(2 * n + 1) / 3; ` `    ``} ` `     `  `    ``// Driver program to test the function. ` ` `  `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int` `n = 10; ` `         `  `        ``Console.WriteLine(weightedMean(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output –

```7
```

This article is contributed by Dharmendra Kumar. 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.