Java Programs - Class XI Computer Project
Series 1(a)
import java.util.Scanner;
public class Series1a {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms: ");
int n = sc.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
int term = (i % 2 == 0) ? 0 : (i - 1);
sum += term;
}
System.out.println("Sum = " + sum);
}
}
Series 1(b)
import java.util.Scanner;
public class Series1b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms: ");
int n = sc.nextInt();
int sum = 0, term = 2;
for (int i = 1; i <= n; i++) {
sum += term;
term *= 2;
}
System.out.println("Sum = " + sum);
}
}
Series 1(c)
import java.util.Scanner;
public class Series1c {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms: ");
int n = sc.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
int term = (int)Math.pow(i, 3);
sum += term;
}
System.out.println("Sum = " + sum);
}
}
Series 2(a)
import java.util.Scanner;
public class Series2a {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms: ");
int n = sc.nextInt();
double sum = 0;
for (int i = 2; i < n + 2; i++) {
double term = 1.0 / (i * i);
sum += (i % 2 == 0) ? term : -term;
}
System.out.println("Sum = " + sum);
}
}
Series 2(b)
import java.util.Scanner;
public class Series2b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of a: ");
double a = sc.nextDouble();
double sum = 1;
for (int i = 2; i <= 10; i++) {
sum += a / i;
}
System.out.println("Sum = " + sum);
}
}
Series 2(c)
public class Series2c {
public static void main(String[] args) {
int fact = 1, sumNum = 0;
double sum = 0;
for (int i = 1; i <= 10; i++) {
fact *= i;
sumNum += i;
sum += (double)fact / sumNum;
}
System.out.println("Sum = " + sum);
}
}
Series 2(d)
import java.util.Scanner;
public class Series2d {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of a: ");
double a = sc.nextDouble();
System.out.print("Enter number of terms: ");
int n = sc.nextInt();
double sum = 0;
for (int i = 1; i <= n; i++) {
int power = 2 * i;
double term = Math.pow(a, power) / (2 * i);
sum += (i % 2 == 1) ? term : -term;
}
System.out.println("Sum = " + sum);
}
}