Open In App

Program to calculate Root Mean Square

Last Updated : 18 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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;
}


Java




// 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


Python3




#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


C#




// 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


PHP




<?php
// PHP program to calculate Root
// Mean Square
 
// Function that Calculate Root
// Mean Square
function rmsValue($arr, $n)
{
    $square = 0;
    $mean = 0.0;
    $root = 0.0;
 
    // Calculate square.
    for ($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
$arr = array( 10, 4, 6, 8 );
$n = sizeof($arr);
 
echo rmsValue($arr, $n);
 
// This code is contributed
// by jit_t
?>


Javascript




<script>
 
// javascript program to calculate
// Root Mean Square
 
    // Function that Calculate Root
    // Mean Square
    function rmsValue(arr , n) {
        var square = 0;
        var mean = 0;
        var root = 0;
 
        // Calculate square.
        for (i = 0; i < n; i++) {
            square += Math.pow(arr[i], 2);
        }
 
        // Calculate Mean.
        mean = (square /  (n));
 
        // Calculate Root.
        root =  Math.sqrt(mean);
 
        return root;
    }
 
    // Driver Code
     
        var arr = [ 10, 4, 6, 8 ];
 
        var n = arr.length;
 
        document.write(rmsValue(arr, n).toFixed(5));
 
// This code contributed by aashish1995
 
</script>


Output: 

7.34847

 

Time Complexity: O(n) where n is size of given array, because for loop would run for all elements of array
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads