Computer Science, asked by anshu12310, 1 year ago

wap to compute the sum of 1!+2!+3!+_ _ _ _n!


angel974: naaa

Answers

Answered by Rajdeep11111
5

HELLO THERE!


I'm assuming that the program code is to be written in Java.

Here I'm using Scanner class to take n as input.

Let's check the code!


import java.util.*;

class Series

{

public static void main (String args[])

{

Scanner sc = new Scanner (System.in);

int n, sum = 0;

Series obj = new Series();

System.out.print("Enter the number of terms: ");

n = sc.nextInt();

for (int i = 1; i <= n; i++)

{

sum = sum + (obj.Fact(i));

}

System.out.println("The sum of the series is: " + sum);

}


public int Fact (int x)

{

int f = 1, i;

for (i = 1; i <= f; i++)

{

f = f * i;

}

return (f);

}

}


So that's the code...

I have used an integer type method which accepts a number i from the main() and calculates the factorial of the number, and then sends the factorial back!


THANKS!


Anonymous: nicely written!
Rajdeep11111: :) thanks bhai
Answered by Anonymous
0

Code :

import java.util.*;


class factorial_number


{


   public static void main(String Args[])


   {


       Scanner sc=new Scanner(System.in);


       System.out.println("Enter the last term upto where i would print");


       int n=sc.nextInt();


       int f = 1;


       int s=0;

       for(int i=1;i<=n;i++)


       {


          f=1;

          for(int j=1;j<=i;j++)

           f=f*i;


           s=s+f;

       }


       System.out.println("Factorial sum of all numbers="+s);


   }


}


__________________________________________

Sample input :

n=4

_____________________________________________

Working :

s = 1! + 2! + 3! + 4!

   = 1+2+6+24

   =33

____________________________________________

Output:

33

__________________________________________

Explanation

First the for loop starts from 1 goes till n.

During its course there is an inner j-loop as well.

This j-loop calculates the factorial of the value of the current i-loop.

for eg: when i=5 , f calculates 1×2×3×4×5

then we add s with f.

this helps in adding all the values of f for all values of i.

____________________________________________

Hope it helps you:-)

If u have any doubts, ask me ^_^

____________________________________________________________________    


Rajdeep11111: Great answer!!
Anonymous: thank u:-)
Similar questions