Skip to content
Related Articles

Related Articles

Improve Article

Program for weighted mean of natural numbers.

  • Difficulty Level : Easy
  • Last Updated : 30 Apr, 2021

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[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
   W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + 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[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
   W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + 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

A simple solution to solve weighed mean. 



C++




// Program to find weighted mean of
// natural numbers.
#include<bits/stdc++.h>
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[0]);
    int m = sizeof(W)/sizeof(W[0]);
 
    // 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




<?php
// Program to find weighted mean of
// natural numbers.
 
// Function to calculate
// weighted mean.
function weightedMean($X, $W, $n)
{
    $sum = 0; $numWeight = 0;
 
    for ($i = 0; $i < $n; $i++)
    {
        $numWeight = $numWeight +
                     $X[$i] * $W[$i];
        $sum = $sum + $W[$i];
    }
 
    return (float)($numWeight / $sum);
}
 
// Driver Code
 
// Take num array and corresponding weight
// array and initialize it.
$X = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$W = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
 
// Calculate the size of array.
$n = sizeof($X);
$m = sizeof($W);
 
// Check the size of both
// array is equal or not.
if ($n == $m)
    echo(weightedMean($X, $W, $n));
else
    echo("-1");
 
// This code is contributed by Ajit.
?>

Javascript




<script>
 
// Javascript program to find weighted mean
// of natural numbers.
 
// Function to calculate weighted mean.
function weightedMean(X, W, n)
{
    let sum = 0, numWeight = 0;
    
    for(let i = 0; i < n; i++)
    {
        numWeight = numWeight + X[i] * W[i];
        sum = sum + W[i];
    }
    
    return (numWeight) / sum;
}
 
// Driver code
 
// Take num array and corresponding
// weight array and initialize it.
let X = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
let W = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
 
// Calculate the size of array.
let n = X.length;
let m = W.length;
 
// Check the size of both array is
// equal or not.
if (n == m)
    document.write(weightedMean(X, W, n));
else
    document.write("-1" );
     
// This code is contributed by susmitakundugoaldanga
 
</script>

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[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
      W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + 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<bits/stdc++.h>
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.

Python3




# 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




<?php
// 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}
function weightedMean($n)
{
    return (2 * $n + 1) / 3;
}
 
// Driver Code
$n = 10;
echo(weightedMean($n));
 
// This code is contributed by Ajit.
?>

Javascript




<script>
    // 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}
    function weightedMean(n)
    {
        return parseInt((2 * n + 1) / 3, 10);
    }
     
    let n = 10;
          
      document.write(weightedMean(n));
         
</script>

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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :