Series programs 3

Series programs 3

https://java4school.com/pattern-programs-in-java-4

Write a program to print the sum of the following series.
s=1^2-2^3+3^4-4^5+…..to n terms

import java.io.*; 
class series4 
{ 
public static void main()throws IOException 
{ 
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("Enter a number of ur choice"); 
int n=Integer.parseInt(br.readLine()); 
int count=1; 
int s=0; 
for(int i=1;i<=n;i++) 
{ 
if(count%2==0) 
{ 
s-=Math.pow(i,i+1); 
} 
else 
{ 
s+=Math.pow(i,i+1); 
} 
count++; 
} 
System.out.println(s); 
} 
} 

Write a program to print the following series.
//1,27,125…… 

import java.io.*; 
class series6 
{ 
public static void main()throws IOException 
{ 
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("Enter a number of ur choice"); 
int n=Integer.parseInt(br.readLine()); 
int sum=0; 
for(int i=1;i<=n;i+=2) 
{ 
int cube=(int)Math.pow(i,3); 
sum+=cube; 
} 
System.out.println(sum); 
} 
} 

Write a program to print the sum of the following series.
//1/2 + 3/4 + 5/6 + ……..19/20


import java.io.*; 
class series8 
{ 
public static void main()throws IOException 
{ 
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("Enter a number of ur choice"); 
int n=Integer.parseInt(br.readLine()); 
int sum=0; 
for(int i=2;i<=20;i+=2) 
{ 
int eq=n/i; 
sum+=eq; 
} 
System.out.println(sum); 
} 
} 

Write a program to print the sum of the following series.
s=1 + a^2! – a^3! + a^4! -…… +a^n

import java.io.*; 
class series_function { 
public static int factorial(int num) 
{ 
int fact=1; 
for(int i=1;i<=num;i++) 
fact*=i; 
return fact; 
} 
public static void main()throws IOException 
{ 
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("Enter a number for ending the power-n"); 
int n=Integer.parseInt(br.readLine()); 
System.out.println("Enter a number-a"); 
int a=Integer.parseInt(br.readLine()); 
int s=0; 
int count=1; 
for(int i=2;i<=n;i++) 
{ 
int b=(int)Math.pow(a,i); 
if(count%2==0) 
s+=factorial(b); 
else 
s-=factorial(b); 
} 
System.out.println(s); 
} } 

https://youtu.be/DjipqnsypVs

LEAVE A REPLY

Please enter your comment!
Please enter your name here