Solve any Sum of series programs in Java
- Categories Java, Iterative Statements, For & While
Watch our video on How to solve any Sum of Series Program in Java – Click Here
1. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class ArithmeticSeries {
public static void main(String[] args) {
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
for(int i=1, a=1; i<=n; i++, a+=2){
sum= sum + a ;
}
System.out.print("Sum : "+sum);
}
}
2. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class ArithmeticSeries {
public static void main(String[] args) {
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
for(int i=1, a=9; i<=n; i++, a+=4){
sum= sum + a ;
}
System.out.print("Sum : "+sum);
}
}
3. Write a java program to print following sum of series:
Code: 5
import java.util.Scanner;
public class ArithmeticSeries {
public static void main(String[] args) {
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
for(int i=1, a=10; i<=n; i++, a--){
sum= sum + a ;
}
System.out.print("Sum : "+sum);
}
}
4. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class GeometricSeries {
public static void main(String[] args) {
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
for(int i=1, k=2; i<=n; i++, k*=3){
sum= sum + k ;
}
System.out.print("Sum : "+sum);
}
}
5. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class GeometricSeries {
public static void main(String[] args) {
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
for(int i=1, k=2; i<=n; i++, k*=2){
sum= sum + k ;
}
System.out.print("Sum : "+sum);
}
}
6. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class GeometricSeries {
public static void main(String[] args) {
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
for(int i=1, k=10; i<=n; i++, k*=3){
sum= sum + k ;
}
System.out.print("Sum : "+sum);
}
}
7. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class GeometricSeries {
public static void main(String[] args) {
double sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
int x= sc.nextInt();
for(int i=1, k=2; i<=n; i++, k*=2){
sum= sum + (double)x/k ;
}
System.out.print("Sum : "+sum);
}
}
8. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class GeometricSeries {
public static void main(String[] args) {
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
for(int i=1, k=2; i<=n; i++, k*=3){
if (i%2==0)
sum= sum - k;
else
sum= sum + k ;
}
System.out.print("Sum : "+sum);
}
}
9. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class SumOfSeries {
public static void main(String[] args) {
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
for(int i=1, k=5; i<=n; i++, k*=5){
sum= sum + k ;
}
System.out.print("Sum : "+sum);
}
}
10. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class ArithmeticSeries {
public static void main(String[] args) {
double sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
int x= sc.nextInt();
for(int i=1, a=1; i<=n; i++, a+=2){
sum= sum + Math.pow(a,3)/x ; // OR sum = sum +(double) a*a*a/x;
}
System.out.print("Sum : "+sum);
}
}
11. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class ArithmeticSeries {
public static void main(String[] args) {
double sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= 10;
int x= sc.nextInt();
for(int i=1, a=2; i<=n; i++, a+=2){
sum= sum + Math.pow(a,x) ;
}
System.out.print("Sum : "+sum);
}
}
12. Write a java program to print following sum of series:
Code: 4
import java.util.Scanner;
public class ArithmeticSeries {
public static void main(String[] args) {
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
for(int i=1, a=1; i<=n; i++, a+=2){
sum= sum + a ;
}
System.out.print("Sum : "+sum);
}
}
13. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class GeometricSeries {
public static void main(String[] args) {
double sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
int x= sc.nextInt();
for(int i=1, a=2, k=10; i<=n; i++, a+=2, k*=3){
sum= sum + (double)(x+a)/k ;
}
System.out.print("Sum : "+sum);
}
}
14. Write a java program to print following sum of series:
Code:
import java.util.Scanner;
public class ArithmeticSeries {
public static void main(String[] args) {
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number :" );
int n= sc.nextInt();
for(int i=1, a=1; i<=n; i++, a+=2){
sum= sum + a ;
}
System.out.print("Sum : "+sum);
}
}
15. Write a java program to print following sum of series:
a1/1 + a2/4 + a3/9 + a4/24 + … + an/n!
Code:
import java.util.Scanner; public class Series2 { public static void main (String[] args) { double n, a, res = 0, prev = 1; Scanner sc = new Scanner(System.in); System.out.print("Enter the value of n: "); n = sc.nextInt(); System.out.print("Value of a: "); a = sc.nextInt(); for (int i = 1; i <= n; i++) { // multiply (a/i) to previous term prev *= (a / i); // store result in res res = res + prev; } System.out.println(res); } }
16. Write a java program to print following sum of series:
1/1 + 2/4 + 3/6 + 4/24 +…+ n
Code:
import java.util.Scanner; public class Series1 { public static void main(String[] args) { double res = 0, fact = 1; Scanner sc = new Scanner(System.in); System.out.print("Input a number : "); int n = sc.nextInt(); for (int i = 1; i <= n; i++) { //fact variable store factorial of the i. fact = fact * i; res = res + (i / fact); } System.out.println("Sum: " + res); } }
17. Write a java program to print following sum of series:
1 + 1/2 + 1/3 + 1/4 + 1/5
Code:
import java.util.Scanner; public class HarmonicSeries { public static void main(String args[]) { int num ; Scanner sc = new Scanner( System.in ); System.out.print("Input a number: "); num = sc.nextInt(); double result = 0.0; while(num > 0){ result = result + (double) 1 / num; num--; } System.out.println("Output of Harmonic Series is "+result); } }
18. Write a program to sum of Harmonic Series :
1 + 1/2 + 1/3 + 1/4 + 1/5
Code:
import java.util.Scanner; public class HarmonicSeries { public static void main(String args[]) { int num ; Scanner sc = new Scanner( System.in ); System.out.print("Input a number: "); num = sc.nextInt(); double result = 0.0; while(num > 0){ result = result + (double) 1 / num; num--; } System.out.println("Output of Harmonic Series is "+result); } }
19. Write a program to find the sum of a Series:
1 + 4 + 9 + 16 + 25 + … + n
Code:
import java.util.Scanner; public class Series3 { public static void main(String[] args) { int i,sum = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter the value of n: "); int n = sc.nextInt(); for (i = 1; i <= n; i++) sum += (i * i); System.out.println(sum); } }9
20. Write a program to print Fibonacci series of n numbers.
Code:
import java.util.Scanner; public class FibonacciSeries { public static void main(String args[]) { int a = 0,b = 1,c,i; System.out.println("Enter the number:"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(i = 1;i<= n;i++) { System.out.print (a+" "); c = a+b; a = b; b = c; } } }
21. Write a java program to print following sum of series:
Code:
import java.util.Scanner; public class GeometricSeries { public static void main(String[] args) { double sum=0; Scanner sc = new Scanner(System.in); System.out.print("Enter Number :" ); int n= sc.nextInt(); int x= sc.nextInt(); for(int i=1, k=5, a1=1, a2=2; i<=n; i++, k*=5, a1++, a2++){ sum= sum + (x*Math.pow(k,2))/(a1+a2) ; } System.out.print("Sum : "+sum); } }
a2 is actually nothing but (a1 + 1) also If you actually take a closer look, both I and a1 are actually doing the same thing, they are initialized by 1 and increment by 1 for each iteration. So actually you do not need a1 too! You can easily substitute I for a1 and your program would look something like this.
import java.util.Scanner; public class GeometricSeries { public static void main(String[] args) { double sum=0; Scanner sc = new Scanner(System.in); System.out.print("Enter Number :" ); int n= sc.nextInt(); int x= sc.nextInt(); for(int i=1, k=5; i<=n; i++, k*=5){ sum= sum + (x*Math.pow(k,2))/(i+i+1) ; } System.out.print("Sum : "+sum); } }
You may also like
Single Linked List Programs in Java
28 August, 2021
Implementing Stack using Array in Java
28 August, 2021
Constructor Programs
3 July, 2021