Advertisement
fahimkamal63

Implementations of series in Java 1.0

Sep 21st, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. /**
  2.  * Implementations of series in Java
  3.  * Date : 21.09.19
  4.  */
  5. package display.series.and.it.s.sum;
  6.  
  7. /**
  8.  *
  9.  * @author Fahim
  10.  */
  11. public class Series {
  12.    
  13.     private int n;
  14.    
  15.     Series(int n) { this.n = n; }
  16.    
  17.     private long power(int num, int p){
  18.         long result = 1;
  19.         for(int i = 1; i <= p; i++){
  20.             result *= num;
  21.         }
  22.         return result;
  23.     }
  24.    
  25.     private void series1(){
  26.         long total = 1;
  27.         System.out.println("The 1st series: \n");
  28.         for(int i = 1; i <= n; i++){
  29.             System.out.print( i +"^2");
  30.             if(i != n) { System.out.print(" X "); }
  31.             total *= power(i, 2);
  32.         }
  33.         System.out.println(" = " + total +"\n\n");
  34.     }
  35.    
  36.     private void series2(){
  37.         long total =  0, num = 3;
  38.         System.out.println("The 2nd series: \n");
  39.         for(int i = 0; i < n; i++){
  40.             System.out.print( num);
  41.             if(i != n-1) { System.out.print(" + "); }
  42.             total += num;
  43.             num += 3;
  44.         }
  45.         System.out.println(" = " + total +"\n\n");
  46.     }
  47.    
  48.     private void series3(){
  49.         long total = 1;
  50.         System.out.println("The 3rd series: \n");
  51.         for(int i = 1; i <= n; i++){
  52.             System.out.print( i +"^3 X " + (i+1));
  53.             if(i != n) { System.out.print(" + "); }
  54.             total += (power(i, 3) * (i + 1));
  55.         }
  56.         System.out.println(" = " + total +"\n\n");
  57.     }
  58.    
  59.     public void run(){
  60.         series1();
  61.         series2();
  62.         series3();
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement