How to generate the series?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajay Bhalala
    New Member
    • Nov 2014
    • 119

    How to generate the series?

    Hello all,

    I want to generate the following series in the java

    1!-2²+3!-4²+5!-6².....

    How can I generate this series in Java?
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    OK, I have down half of your homework.....

    Code:
    class Series {
                public static void main(String[] args) {
                                System.out.println("Hello World!"); // Display the string.
                                int x=1;
                                double s=0;
    
                                while (x<10) {
                                        s = s + factorial(x);
                                        s = s - Math.pow((x+1),2);
                                        x = x + 2;
                                        System.out.println(s);
                                }
          }
    
          public static int factorial(int x) {
                  switch (x) {
                          case 1: return 1;
                          case 3: return 6;
                          case 5: return 120;
                          case 7: return 5040;
                          case 9: return 362880;
                          default: System.out.println("Create your own factorial function....."); return 0;
                  }
          }
    }
    and i'm not even a java programmer..... .

    Comment

    • Ajay Bhalala
      New Member
      • Nov 2014
      • 119

      #3
      Hi Luuk,

      Thank you so much for your reply.

      You have really realeased my tantion about this series. Thank you so much.

      Comment

      Working...