07A-2017 – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2016-07A #JAVA#7325    siteicon   siteicon  

Problem Statement - 07A-2017

Design a class to overload a function SumSeries() as follows: [15]
(i) void SumSeries(int n, double x) – with one integer argument and one double argument to find and display the sum of the series given
below:
s = (x/1) – (x/2) + (x/3) – (x/4) + (x/5) … to n terms
(ii) void SumSeries() – To find and display the sum of the following series:
s = 1 + (1 X 2) + (1 X 2 X 3) + … + (1 X 2 X 3 X 4 X … 20)

Solution

TC++ #7325

public class Series {

 public void SumSeries(int n, double x) {
 double sum = 0;
 for (int i = 1; i <= n; i++) {
 if (i % 2 == 1) {
 sum = sum + (x / i);
 } else {
 sum = sum - (x / i);
 }
 }
 System.out.println("Sum = " + sum);
 }
 public void SumSeries() {
 int sum = 0;
 for (int i = 1; i <= 20; i++) {
 int product = 1;
 for (int j = 1; j <= i; j++) {
 product = product * j;
 }
 sum = sum + product;
 }
 System.out.println("Sum = " + sum);
 }
 }


Share

CSKC| Created: 16-Apr-2019 | Updated: 16-Apr-2019|ICSE2016









Back