How to solve any sum of series program with nested loops
- Categories Java, Iterative Statements, Nested loops
Watch our video on How to solve any sum of series program with nested loops- Click Here
1. Write a java program to print sum of following series:
Code:
import java.util.Scanner; public class NetedLoopSeries { 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, d=9; i<=n; i++, a+=2, d+=4){ int fact=1; for(int j=1; j<=d; j++) fact = fact*j; sum=sum + Math.pow(x,a)/fact; } System.out.println("Sum: "+sum); } } }
2. Write a java program to print following sum of series:
(9! + 13 ! + 17! … N)
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, a=9; i<=n; i++, a+=4{ int fact=1; for(int j=1; j<=a; j++) fact = fact*j; sum = sum+fact; } System.out.println("Sum: "+sum); } }
3. 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);
}
}
4. Write a java program to print following sum of series:
(2! + 4! + 8! + 16! +… N)
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, a=2; i<=n; i++, a*=2{ int fact=1; for(int j=1; j<=a; j++) fact = fact*j; sum = sum+fact; } System.out.println("Sum: "+sum); } }
5. Write a java program to print following sum of series:
(1! + 2 ! + 3! + 4! … N)
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, a=1; i<=n; i++,a++){ int fact=1; for(int j=1; j<=a; j++) fact = fact*j; sum = sum + fact; } System.out.println("Sum: "+sum); } }
6. Write a java program to print following sum of series:
(10! + 9! + 8! + … 1!)
Code:
public class SumOfSeries { public static void main(String[] args) { int sum=0; int n = 10; for(int i=1, a=10; i<=n; i++, a--{ int fact=1; for(int j=1; j<=a; j++) fact = fact*j; sum=sum+fact; } System.out.println("Sum: "+sum); } }
7. Write a java program to print following sum of series:
Code:
import java.util.Scanner; public class SumOfSeries { public static void main(String[] args) { double sum=0; Scanner sc = new Scanner(System.in); System.out.print("Enter Number:"); int n = sc.nextInt(); for(int i=1, a=2; i<=n; i++, a++){ int fact=1; for(int j=1; j<=a; j++) fact = fact*j; sum= sum + double(i+a)/fact; } System.out.println("Sum: "+sum); } }
8. Write a java program to print following sum of series:
(1×2 + 1x2x3 + 1x2x3x4 + … N )
Code:
import java.util.Scanner; public class NetedLoopSeries { 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=2; i<=n; i++,a++{ int fact=1; for(int j=1; j<=a; j++) fact = fact*j; sum=sum+fact; } System.out.println("Sum: "+sum); } }
9. Write a java program to print following sum of series:
Code:
import java.util.Scanner; public class NetedLoopSeries { public static void main(String[] args) { double sum=0; Scanner sc = new Scanner(System.in); System.out.print("Enter Number:"); int n = sc.nextInt(); for(int i=1, a=2; i<=n; i++,a++){ int fact=1; for(int j=1; j<=a; j++) fact = fact*j; sum= sum + (double) (i+a)/fact; } System.out.println("Sum: "+sum); } }
10. Write a java program to print following sum of series:
Code:
import java.util.Scanner; public class SumOfSeries { 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, d=2; i<=n; i++, a+=10, d*=2){ int fact = 1; for(int j=1; j<=d; j++) fact = fact*j; sum = sum + double(a+x)/fact; } System.out.println("Sum: "+sum); } }
11. Write a java program to print following sum of series:
Code:
import java.util.Scanner; public class NetedLoopSeries { public static void main(String[] args) { double sum=0; Scanner sc = new Scanner(System.in); System.out.print("Enter Number:"); int n = sc.nextInt(); for(int i=1, a=2; i<=n; i++,a*=2){ int fact=1; for(int j=1; j<=i+x; j++) fact = fact*j; int fact1 = 1; for(int j=1; j<=a; j++) fact = fact*j; sum= sum + (double) fact/fact1; } System.out.println("Sum: "+sum); } }
12. Write a java program to print following sum of series:
Code:
import java.util.Scanner; public class NetedLoopSeries { 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++{ int fact=1; for(int j=1; j<=a; j++) fact = fact*j; sum= sum + Math.pow(x,a)/fact; } System.out.println("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