Series programs 3
https://wordpress-343193-1101484.cloudwaysapps.com/pattern-programs-in-java-4
Series programs 3
Pattern programs in java part 1: Simple loop programs
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
Related Posts
Continue learning with these related articles
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.