Program to calculate Root Mean Square

Given an array of N numbers. The task is to calculate the Root Mean Square(RMS) of the given numbers.

Examples:

Input: arr[] = {1, 2, 3, 4, 5}
Output: 3.31662

Input: arr[] = {10, 4, 6, 8}
Output: 7.34847

Approach: The Root Mean Square value of N numbers x1,x2,x3,…..xn can be given as,

RMS method first calculates the square of each number and then calculate the mean and finally calculate the square root of the mean.

Below is the program to find RMS of N numbers:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to calculate Root Mean Square
#include <bits/stdc++.h>
using namespace std;
  
// Function that Calculate Root Mean Square
float rmsValue(int arr[], int n)
{
    int square = 0;
    float mean = 0.0, root = 0.0;
  
    // Calculate square.
    for (int i = 0; i < n; i++) {
        square += pow(arr[i], 2);
    }
  
    // Calculate Mean.
    mean = (square / (float)(n));
  
    // Calculate Root.
    root = sqrt(mean);
  
    return root;
}
  
// Driver code
int main()
{
    int arr[] = { 10, 4, 6, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << rmsValue(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to calculate 
// Root Mean Square 
  
class GFG 
{
  
// Function that Calculate Root
// Mean Square 
static float rmsValue(int arr[], int n)
{
    int square = 0;
    float mean = 0;
    float root = 0;
  
    // Calculate square.
    for(int i = 0; i < n; i++)
    {
        square += Math.pow(arr[i], 2);
    }
      
    // Calculate Mean. 
    mean = (square / (float) (n));
  
    // Calculate Root.
    root = (float)Math.sqrt(mean);
  
    return root;
}
  
  
// Driver Code 
public static void main(String args[])
{
    int arr[] = { 10, 4, 6, 8};
      
    int n = arr.length;
      
    System.out.println(rmsValue(arr, n));
}
}
  
// This code is contributed by ANKITRAI1

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

#Python3 program to calculate Root Mean Square 
  
import math 
#Function that Calculate Root Mean Square 
def rmsValue(arr, n):
    square = 0
    mean = 0.0
    root = 0.0
      
    #Calculate square
    for i in range(0,n):
        square += (arr[i]**2)
      
    #Calculate Mean 
    mean = (square / (float)(n))
      
    #Calculate Root
    root = math.sqrt(mean)
      
    return root
  
#Driver code
if __name__=='__main__':
    arr = [10, 4, 6, 8]
    n = len(arr)
    print(rmsValue(arr, n))
  
#This code is contributed by Shashank_Sharma

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to calculate 
// Root Mean Square 
using System; 
class GFG 
{
  
// Function that Calculate 
// Root Mean Square 
static float rmsValue(int[] arr, int n)
{
    int square = 0;
    float mean = 0;
    float root = 0;
  
    // Calculate square.
    for(int i = 0; i < n; i++)
    {
        square += (int)Math.Pow(arr[i], 2);
    }
      
    // Calculate Mean. 
    mean = (square / (float) (n));
  
    // Calculate Root.
    root = (float)Math.Sqrt(mean);
  
    return root;
}
  
// Driver Code 
public static void Main()
{
    int[] arr = {10, 4, 6, 8};
      
    int n = arr.Length;
      
    Console.Write(rmsValue(arr, n));
}
}
  
// This code is contributed
// by ChitraNayal

chevron_right


PHP

Output:

7.34847


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.