Calculate Root Mean Kth power of all array elements | C # program for the above approach ; Function to find the Nth root ; Instantiate random number generator ; Initially guessing random numberbetween 0 and 9 ; Smaller eps for more accuracy ; Initialize difference between the two roots by Integer . MAX_VALUE ; xK denotes current value of x ; Iterate until desired accuracy is reached ; Find the current value from previous value by newton 's method ; Function to calculate the Root Mean kth power of array elements ; Calculate sum of kth power ; Calculate Mean ; Calculate kth Root of mean ; Driver Code ; Function call
using System ; class GFG { static double nthRoot ( int A , int N ) { Random rand = new Random ( ) ; double xPre = ( rand . Next ( ) * 10 ) % 10 ; double eps = 1e-3 ; double delX = Int32 . MaxValue ; double xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . Pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . Abs ( xK - xPre ) ; xPre = xK ; } return xK ; } static float RMNValue ( int [ ] arr , int n , int k ) { int Nth = 0 ; float mean = 0 , root = 0 ; for ( int i = 0 ; i < n ; i ++ ) { Nth += ( int ) Math . Pow ( arr [ i ] , k ) ; } mean = ( Nth / ( float ) ( n ) ) ; root = ( float ) nthRoot ( ( int ) mean , k ) ; return root ; } public static void Main ( ) { int [ ] arr = { 10 , 4 , 6 , 8 } ; int N = arr . Length ; int K = 3 ; Console . Write ( RMNValue ( arr , N , K ) ) ; } }