Computer Science, asked by dashankit1998, 7 months ago

The function printSeries(int S, int D, int T, char ch) accepts integer S as the starting tern
series, integer D as the common difference for AP or common ratio for GP, integer T as
number of terms and a character ch as the series type to print. It is supposed to print th
arithmetic progression if the character ch is 'A' and print the geometric progression if th
character ch is 'G'.
The program must calculate hoth arithmetic progression if the character chis 'A' and print the geometric progression if the ch is 'G'.​

Answers

Answered by Oreki
0

public class Progressions {

static void printAP(int a, int n, int d) {

System.out.print((a + ((n - 1) * d)) + " ");

}

static void printGP(int a, int n, int r) {

System.out.print((int)(a * Math.pow(r, n - 1)) + " ");

}

static void printSeries(int S, int D, int T, char ch) {

switch (Character.toUpperCase(ch)) {

case 'A':

System.out.println("Required AP - ");

for (int i = 1; i <= T; i++)

printAP(S, i, D);

System.out.println( );

break;

case 'G':

System.out.println("Required GP - ");

for (int i = 1; i <= T; i++)

printGP(S, i, D);

System.out.println( );

break;

default:

System.out.println("Wrong Choice!!");

}

}

public static void main(String[ ] args) {

printSeries(1, 2, 10, 'A');

printSeries(1, 2, 10, 'g');

printSeries(1, 2, 10, 'Z');

}

}

Output:

Required AP -

1 3 5 7 9 11 13 15 17 19

Required GP -

1 2 4 8 16 32 64 128 256 512

Wrong Choice!!

Similar questions