Given N and Standard Deviation , find N elements | Java program to find n elements ; function to print series of n elements ; if S . D . is 0 then print all elements as 0. ; print n 0 's ; if S . D . is even ; print - SD , + SD , - SD , + SD ; if odd ; convert n to a float integer ; print one element to be 0 ; print ( n - 1 ) elements as xi derived from the formula ; driver function
import java . util . * ; import java . lang . * ; public class GfG { public static void series ( int n , int d ) { if ( d == 0 ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( "0 ▁ " ) ; System . out . println ( ) ; return ; } if ( n % 2 == 0 ) { for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( Math . pow ( - 1 , i ) * d + " ▁ " ) ; } System . out . println ( ) ; } else { float m = n ; float r = ( m / ( m - 1 ) ) ; float g = ( float ) ( d * ( float ) ( Math . sqrt ( r ) ) ) ; System . out . print ( "0 ▁ " ) ; for ( int i = 1 ; i < n ; i ++ ) { System . out . print ( Math . pow ( - 1 , i ) * g + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { int n = 3 , d = 3 ; series ( n , d ) ; } }