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++
// 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
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
PHP
Output:
7.34847
Recommended Posts:
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Program to calculate area of an Circle inscribed in a Square
- Check if a number is perfect square without finding square root
- Square root of an integer
- Square root of a number using log
- Babylonian method for square root
- Fast inverse square root
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Square root of a number without using sqrt() function
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Euler's criterion (Check if square root under modulo p exists)
- Find square root of number upto given precision using binary search
- Program to print Square inside a Square
- Program to calculate the value of sin(x) and cos(x)
- Program to calculate value of nCr
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.



