Page 201 - Cs_withBlue_J_C11_Flipbook
P. 201
31 for(i=1; i<=n; i++)
32 {
33 s=s*10+1;
34 System.out.print(s+ " , ");
35 }
36 break;
37 default: System.out.println("Wrong Choice");
38 }
39 }
40 }
The output of the preceding program is as follows:
a. s = 1 - 4 + 9 - 16 + ………… to n terms
b. 1, 11, 111, 1111, ……..… to n terms
Enter your choice
a
th
Enter n term
5
Sum = 15
Program 11 Write a program in Java to find the sum of the given series after accepting the value of ‘n’
from the user.
1
1
1
1. s = 1 + + + ….. +
2 3 n
1
4
2
3
2. s = + + + + …. + to n terms
a a a a
1 import java.util.*;
2 class series2
3 {
4 public static void main()
5 {
6 Scanner sc= new Scanner(System.in);
7 int ch, n, i;
8 double s=0, a;
9 System.out.println("1. s = 1 + 1/2 + 1/3 + ..... 1/n");
10 System.out.println("2. s = 1/a + 2/a + 3/a + 4/a +.... + to n terms ");
11 System.out.println("Enter your choice ");
199
Statements and Scope 199

