Computer Applications
Using switch statement write a menu driven program to overload a function series as follows:
(a) void series() — To find and display the sum of the following series:
S = 2 - 4 + 6 - 8 + 10 ….. - 20
(b) void series(int n, int x) — To find and display the sum of the following series:
S = (x2 / 1!) + (x4 / 3!) + (x6 / 5!) + ……. to n terms
Java
User Defined Methods
21 Likes
Answer
import java.util.Scanner;
public class KboatSeries
{
void series() {
int sum = 0;
int t = 1;
for (int i = 2; i <= 20; i += 2) {
sum += t * i;
t *= -1;
}
System.out.println("Sum = " + sum);
}
void series(int n, int x) {
double sum = 0;
for (int i = 2; i <= 2 * n; i += 2) {
long f = 1;
for (int j = 1; j <= i - 1; j++) {
f *= j;
}
double t = Math.pow(x, i) / f;
sum += t;
}
System.out.println("Sum = " + sum);
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
KboatSeries obj = new KboatSeries();
System.out.println("Enter 1 for Series 1");
System.out.println("Enter 2 for Series 2");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
obj.series();
break;
case 2:
System.out.print("Enter x: ");
int x = in.nextInt();
System.out.print("Enter no. of terms(n): ");
int n = in.nextInt();
obj.series(n, x);
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Output


Answered By
9 Likes
Related Questions
Invoking a method by passing the objects of a class is termed as:
- Call by reference
- Call by value
- Call by method
- Call by constructor
Method prototype for the method compute which accepts two integer arguments and returns true/false.
- void compute (int a, int b)
- boolean compute (int a, int b)
- Boolean compute (int a,b)
- int compute (int a, int b)
Define a class to overload the method perform as follows:
double perform (double r, double h) — to calculate and return the value of curved surface area of cone
void perform (int r, int c) — Use NESTED FOR LOOP to generate the following format
r = 4, c = 5
output
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5void perform (int m, int n, char ch) — to print the quotient of the division of m and n if ch is Q else print the remainder of the division of m and n if ch is R
State a difference between call by value and call by reference.