1.

Write a program to calculate and print the sum of each of the following series:(a) sum (S) = 2-4+6-8+.....-20(b) Sum (S) = x/2 + x/5 + x/8 + x/11 + .... + x/20 (Value of x to be input by the user).

Answer»

(a)

class series 

public static void main() 

int s=0; 

for(int i=2; i<=20; i=i+2)

if(i%4==0) 

{ s=s-i; 

else 

s=s+i; 

System.out.println(“The Sum of the Series is”+s); 

}

(b)

class series 

public static void main() throws IOException 

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 

System.out.println(“Enter the value of x:”); 

int x = Integer.parseInt(br.readLine( )); 

double s=0; 

for(int i=2; i<=20; i=i+3) 

s=s+(double)x/i; 

System.out.println(“The Sum of the Series is”+s); 

}



Discussion

No Comment Found

Related InterviewSolutions