System.out.print(i*sign+" "); //*sign changes sign of value
sign = - sign; //sign changes + and - alternately
}
}
}
Do Yourself
Print series 1 –3 5 –7 .... +/-n.
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n");
int n = sc.nextInt();
int sign=1; //sign is for + and -
for(int i=1;i<=n;i+=2)
{
System.out.print(i*sign+" "); //*sign changes sign of value
sign = - sign; //sign changes + and - alternately
}
}
}
Do Yourself
Print series 1 –3 5 –7 .... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n");
int n = sc.nextInt();
int a=1,sign=1; //a is the series
for(int i=1;i<=n;i++) //i is the counter
{
System.out.print(a*sign+" "); //note the a*sign
a+=2;
sign = - sign;
}
}
}
Do Yourself
Print series 2 –5 8 –11 .... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n");
int n = sc.nextInt();
int a=2,sign=1; //a is the series
for(int i=1;i<=n;i++) //i is the counter
{
System.out.print(a*sign+" "); //note the a*sign
a+=3;
sign = - sign;
}
}
}
Do Yourself
Print sum of series 1 – 2 + 3 – 4 …. +/- n
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n");
int n = sc.nextInt();
int s=0, sign=1; //sign for + and -
for(int i=1;i<=n;i++)
{
s=s+i*sign; //expression changes sign
sign = - sign; //sign changes + and - alternately
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series 1 – 3 + 5 – 7 …. +/- n
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n");
int n = sc.nextInt();
int s=0, sign=1;
for(int i=1;i<=n;i+=2)
{
s=s+i*sign;
sign = - sign;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series 1 – 3 + 5 – 7 …. n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n = sc.nextInt();
int s=0, a=1,sign=1;
for(int i=1;i<=n;i++)
{
s=s+a*sign;
a+=2;
sign = - sign;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series 2 – 5 + 8 – 11 …. n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n = sc.nextInt();
int s=0, a=2,sign=1;
for(int i=1;i<=n;i++)
{
s=s+a*sign;
a+=3;
sign = - sign;
}
System.out.println("Sum="+s);
}
}
3. Math.pow() and X for Series with Base and Exponent
Learn it
Print sum of series x1 + x2 + x3 + x4 …… xn
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
int x=sc.nextInt(); //x - base
int n=sc.nextInt();
double s=0.0;
for(int i=1;i<=n;i++) //i - power/exponent
s=s+Math.pow(x,i); //finds x1 + x2 + ...
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series x1 + x3 + x5 + x7 …… xn
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
int x=sc.nextInt();
int n=sc.nextInt();
double s=0.0;
for(int i=1;i<=n;i+=2) //power increments by 2
s=s+Math.pow(x,i);
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series x1 + x3 + x5 + x7 …… n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and number of terms");
int x=sc.nextInt();
int n=sc.nextInt();
double s=0;
int a=1; //power(exponent)
for(int i=1;i<=n;i++) //counter
{
s=s+Math.pow(x,a);
a+=2;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series x2 + x5 + x8 + x11 …… n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and number of terms");
int x=sc.nextInt();
int n=sc.nextInt();
double s=0.0;
int a=2; //power(exponent)
for(int i=1;i<=n;i++) //counter
{
s=s+Math.pow(x,a);
a+=3;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series x1 - x3 + x5 - x7 …… xn
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
int x=sc.nextInt();
int n=sc.nextInt();
double s=0, sign=1; //the sign can be int also
for(int i=1;i<=n;i+=2)
{
s=s+Math.pow(x,i)*sign;
sign = - sign;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series x2 - x5 + x8 - x11 …… xn
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
int x=sc.nextInt();
int n=sc.nextInt();
double s=0, sign=1; //sign for + and -
for(int i=2;i<=n;i+=3)
{
s=s+Math.pow(x,i)*sign;
sign = - sign;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series x1 - x3 + x5 - x7 …… n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and number of terms");
int x=sc.nextInt();
int n=sc.nextInt();
double s=0, a=1, sign=1; //a is the series
for(int i=1;i<=n;i++) //i is the counter
{
s=s+Math.pow(x,a)*sign;
a+=2;
sign = - sign;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series x2 - x5 + x8 - x11 …… n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and number of terms");
int x=sc.nextInt();
int n=sc.nextInt();
double s=0, a=2, sign=1;
for(int i=1;i<=n;i++)
{
s=s+Math.pow(x,a)*sign;
a+=3;
sign = - sign;
}
System.out.println("Sum="+s);
}
}
4. Numerator and Denominator in Series
Learn it
Print sum of series 1/2 + 2/5 + 3/8+ ... n/m
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n");
int n = sc.nextInt();
double m=2,s=0.0; //m - denominator; should be double
for(int i=1;i<=n;i++) //i - numerator increments by 1
{
s=s+i/m;
m+=3; //increments by 2
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series 2/3 + 4/6 + 6/9+ ... n/m
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n");
int n = sc.nextInt();
double m=2,s=0.0;
for(int i=2;i<=n;i+=2)
{
s=s+i/m;
m+=3;
}
System.out.println("Sum="+s);
}
}
Learn it
Print sum of series 1/2 + 3/5 + 5/8+ ... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n = sc.nextInt();
double a=1,m=2,s=0; //a - numerator, m - denominator
for(int i=1;i<=n;i++) //i - to count number of terms
{
s=s+a/m;
a+=2;
m+=3;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series 2/1 + 5/3 + 8/5+ ... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n = sc.nextInt();
double a=2,m=1,s=0.0; //a - numerator, m - denominator
for(int i=1;i<=n;i++) //i - to count
{
s=s+a/m;
a+=3;
m+=2;
}
System.out.println("Sum="+s);
}
}
5. All are Joined Together
Learn it
Find sum of series s=(x1/2) + (x2/4) + (x3/6) + (x4/8) …… (xn/m)
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
double x=sc.nextDouble(); //x - base
int n=sc.nextInt(); //n - last exponent
double s=0,m=2; //m - denominator
for(int i=1;i<=n;i++) //i - exponents increments by 1
{
s=s+Math.pow(x,i)/m;
m+=2; //increments by 2
}
System.out.println("Sum="+s);
}
}
Do Yourself
Find sum of series s=(x/2) + (x3/5) + (x5/8) + (x7/11) ... (xn/m)
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
double x=sc.nextDouble();
int n=sc.nextInt();
double s=0.0,m=2;
for(int i=1;i<=n;i+=2)//increments by 2
{
s=s+Math.pow(x,i)/m;
m+=3; //increments by 3
}
System.out.println("Sum="+s);
}
}
Learn it
Find sum of series s=(x1/2) + (x3/5) + (x5/8) + (x7/11) …… n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and number of terms");
double x=sc.nextDouble(); //x - base
int n=sc.nextInt(); //n - number of terms
int a=1; //a - exponent
double s=0,m=2; //m - denominator
for(int i=1;i<=n;i++) //i - to count series
{
s=s+Math.pow(x,a)/m;
a+=2; //increments by 2
m+=3; //increments by 3
}
System.out.println("Sum="+s);
}
}
Do Yourself
Find sum of series s=(x/2) + (x4/4) + (x7/6) + (x10/8) ... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and number of terms");
double x=sc.nextDouble();
int n=sc.nextInt();
int a=1;
double s=0.0,m=2;
for(int i=1;i<=n;i++)
{
s=s+Math.pow(x,a)/m;
a+=3; //increments by 3
m+=2; //increments by 2
}
System.out.println("Sum="+s);
}
}
Learn it
Find sum of series s=(x1/2) – (x2/4) + (x3/6) – (x4/8) …… (xn/m)
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
double x=sc.nextDouble(); //x - base
int n=sc.nextInt(); //last exponent
double s=0,m=2,sign=1; //sign for + and -
for(int i=1;i<=n;i++) //i - exponent
{
s=s+(Math.pow(x,i)/m)*sign; //to change + and - of expression
m+=2;
sign=-sign;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Find sum of series s=(x/2) – (x3/5) + (x5/8) – (x7/11) ... (xn/m)
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
double x=sc.nextDouble();
int n=sc.nextInt();
double s=0.0,m=2,sign=1;
for(int i=1;i<=n;i+=2)
{
s=s+Math.pow(x,i)/m*sign;
m+=3;
sign=-sign;
}
System.out.println("Sum="+s);
}
}
Learn it
Find sum of series s=(x/2) – (x3/5) + (x5/8) – (x7/11) …… n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
double x=sc.nextDouble(); //x - base
int n=sc.nextInt(); //last exponent
double s=0,a=1,m=2,sign=1; //sign for + and -
for(int i=1;i<=n;i++) //i - counter
{
s=s+Math.pow(x,a)/m*sign; //to change + and - of expression
a+=2;
m+=3;
sign=-sign; //+ and - change alternately
}
System.out.println("Sum="+s);
}
}
Do Yourself
Find sum of series s=(x2/3) – (x4/6) + (x6/9) – (x8/12) ... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
double x=sc.nextDouble();
int n=sc.nextInt();
double s=0.0,a=2,m=3,sign=1;
for(int i=1;i<=n;i++)
{
s=s+Math.pow(x,a)/m*sign;
sign=-sign;
a+=2;
m+=3;
}
System.out.println("Sum="+s);
}
}
6. Factorial of a Number is Introduced
Learn it
Find Factorial of a number. Product of series 1 to the number.
Factorial of 1 is 1. (1x1=1)
Fatctorial of 5 is 120 (1x1=1, 1x2=2, 2x3=6, 6x4=24, 24x5=120)
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n");
int n=sc.nextInt();
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
System.out.println("Factorial = "+f);
}
}
7. Nested Loop Series
Following exercises upto 47 are continuation of above exercises.
Remaining single loop exercises continues from 48.
Learn it
Print sum of series 1! + 2! + 3! + 4! + ... + n!
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n");
int n=sc.nextInt();
int s=0;
for(int i=1;i<=n;i++) //1 2 3 4 ... n of 1! 2! 3! ... n!
{
int f=1;
for(int j=1;j<=i;j++) //j loop finds the factorial of series
f=f*j;
s=s+f;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series 2! + 5! + 8! + 11! + …… + n!
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n");
int n=sc.nextInt();
int s=0;
for(int i=2;i<=n;i+=3)
{
int f=1;
for(int j=1;j<=i;j++)
f=f*j;
s=s+f;
}
System.out.println("Sum="+s);
}
}
Learn it
Print sum of series 1! + 3! + 5! ... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n=sc.nextInt();
int a=1,s=0; //a - for series
for(int i=1;i<=n;i++) //i- counter
{
int f=1;
for(int j=1;j<=a;j++) //j loop finds the factorial of series
f=f*j;
s=s+f;
a+=2;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Print sum of series 2! + 5! + 8! ... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n");
int n=sc.nextInt();
int a=2,s=0;
for(int i=1;i<=n;i++)
{
int f=1;
for(int j=1;j<=a;j++)
f=f*j;
s=s+f;
a+=3;
}
System.out.println("Sum="+s);
}
}
Learn it
Find sum of series s=(x1/2!) + (x2/4!) + (x3/6!) + (x4/8!) …… (xn/m!)
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
double x=sc.nextDouble(); //x - base
int n=sc.nextInt(); //n - last exponent
double s=0,m=2; //m - denominator
for(int i=1;i<=n;i++) //i - exponents increments by 1
{
int f=1;
for(int j=1;j<=m;j++) //factorial of m
f=f*j;
s=s+Math.pow(x,i)/f;
m+=2; //increments by 2
}
System.out.println("Sum="+s);
}
}
Do Yourself
Find sum of series s=(x/2!) + (x3/5!) + (x5/8!) + (x7/11!) ... (xn/m!)
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
double x=sc.nextDouble();
int n=sc.nextInt();
double s=0.0,m=2;
for(int i=1;i<=n;i+=2)//increments by 2
{
int f=1;
for(int j=1;j<=m;j++)
f*=j;
s=s+Math.pow(x,i)/f;
m+=3; //increments by 3
}
System.out.println("Sum="+s);
}
}
Learn it
Find sum of series s=(x1/2!) – (x2/4!) + (x3/6!) – (x4/8!) …… (xn/m!)
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
double x=sc.nextDouble(); //x - base
int n=sc.nextInt(); //last exponent
double s=0,m=2,sign=1; //sign for + and -
for(int i=1;i<=n;i++) //i - exponent
{
int f=1;
for(int j=1;j<=m;j++) //factorial of m
f=f*j;
s=s+(Math.pow(x,i)/f)*sign; //to change + and - of expression
sign=-sign; //+ and - change alternately
m+=2;
}
System.out.println("Sum="+s);
}
}
Do Yourself
Find sum of series s=(x/2!) – (x3/5!) + (x5/8!) – (x7/11!) ... (xn/m!)
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
double x=sc.nextDouble();
int n=sc.nextInt();
double s=0.0,m=2,sign=1;
for(int i=1;i<=n;i+=2)
{
int f=1;
for(int j=1;j<=m;j++)
f*=j;
s=s+(Math.pow(x,i)/f)*sign;
sign=-sign;
m+=3;
}
System.out.println("Sum="+s);
}
}
Learn it
Find sum of series s=(x/2!) – (x3/5!) + (x5/8!) – (x7/11!) ... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and number of terms");
Find sum of series s=1/x – 3!/x4 + 5!/x7 – 7!/x10 ... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and number of terms");
double x=sc.nextDouble();
int n=sc.nextInt();
double s=0.0,a=1,m=1,sign=1;
for(int i=1;i<=n;i++)
{
int f=1;
for(int j=1;j<=m;j++)
f*=j;
s=s+f/Math.pow(x,a)*sign;
sign=-sign;
a+=3; //a - exponent increments by 3
m+=2; //m - denominator increments by 2
}
System.out.println("Sum="+s);
}
}
8. Single Loop Series Returns
1. Fibonacci Series
Learn it
Print Fibonacci Series 0 1 1 2 3 5 8 13 .... n terms. Initial two terms are 0 and 1. Third is sum
of previous 0 and 1, i.e, 1. Next is sum of previous two terms. 1+1 i.e, 2 ...
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n=sc.nextInt();
int a=0,b=1,c;
for(int i=1;i<=n;i++)
{
System.out.print(a+" "); //0
c=a+b;//c=1(0+1)
a=b; //a=1
b=c; //b=1
}
}
}
Learn it
Find sum of Series 0 + 1 + 1 + 2 + .... n terms.
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n=sc.nextInt();
int a=0,b=1,c,s=0;
for(int i=1;i<=n;i++)
{
s=s+a;
c=a+b;
a=b;
b=c;
}
System.out.println("Sum: "+s);
}
}
2. Series 1 11 111 ... and 1 12 123 ...
Learn it
Print series 1 11 111 ..... 111111111
public class Series
{
public static void main()
{
for(long i=1;i<=111111111L;i=i*10+1)
System.out.print(i+" ");
}
}
Do Yourself
Print series 5 55 555 ..... 555555555
public class Series
{
public static void main()
{
for(long i=5;i<=555555555L;i=i*10+5)
System.out.print(i+" ");
}
}
Learn it
Print sum of series 1 + 11 + 111 + ..... + 111111111
public class Series
{
public static void main()
{
long s=0;
for(long i=1;i<=111111111L;i=i*10+1)
s=s+i;
System.out.println("Sum = "+s);
}
}
Do Yourself
Print sum of series 5 + 55 + 555 + ..... + 555555555
public class Series
{
public static void main()
{
long s=0;
for(long i=5;i<=555555555L;i=i*10+5)
s=s+i;
System.out.println("Sum = "+s);
}
Learn it
Print series 1 11 111 ..... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n=sc.nextInt();
long a=1; //a - series
for(int i=1;i<=n;i++) //i - counter increments by 1
{
System.out.print(a+" "); //a is printed
a=a*10+1; //a makes series
}
}
}
Do Yourself
Print series 5 55 555 ..... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n=sc.nextInt();
long a=5;
for(int i=1;i<=n;i++)
{
System.out.print(a+" ");
a=a*10+5;
}
}
}
Learn it
Print sum of series 1 + 11 + 111 ..... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n=sc.nextInt();
long a=1; //a - series
long s=0;
for(int i=1;i<=n;i++) //i - counter increments by 1
{
s=s+a; //a is added
a=a*10+1; //a makes series
}
System.out.print("Sum = "+s);
}
}
Do Yourself
Print sum of series 5 + 55 + 555 ..... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n=sc.nextInt();
long a=5;
long s=0;
for(int i=1;i<=n;i++)
{
s=s+a; //a is added
a=a*10+5;
}
System.out.print("Sum = "+s);
}
}
Learn it
Print series 1 12 123 ..... 123456789
public class Series
{
public static void main()
{
long a=2; //a - last digits 2, 3, 4, 5 ...
for(int i=1;i<=123456789;i=i*10+a++) //i - series
System.out.print(i+" ");
}
}
Learn it
Print series 1 12 123 ..... n terms
import java.util.*;
public class Series
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of terms");
int n=sc.nextInt();
long a=2, b=1; //b - series, a - last digits
for(int i=1;i<=n;i++) //i - counter
{
System.out.print(b+" "); //b - series prints
b=b*10+a++; //b updates to series by adding a (last digit)
}
}
}
Learn it
Print sum of series 1 + 12 + 123 + ..... + 123456789
public class Series
{
public static void main()
{
long s=0;
int a=2; //a - last digits 2, 3, 4, 5 ...
for(long i=1;i<=123456789L;i=i*10+a++) //i - series