forum

Home / DeveloperSection / Forums / My Factorial program isn’t giving correct output using java

My Factorial program isn’t giving correct output using java

Anonymous User174905-Oct-2013
import java.math.BigInteger; 
import java.io.*;

class Factorial
{
    public static void main(String args[])
    {
        try
        {
            int key;
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            BigInteger result=new BigInteger("1");
            //take the quantity of elements for which factorial is to be calculated
            int num=Integer.parseInt(br.readLine());

            while(num-->0)
            {
                //input the number for which factorial is to be calculated
                key=Integer.parseInt(br.readLine());
                while(key>0)
                {
                    System.out.println(key+""+result);
                    result.multiply(BigInteger.valueOf(key));
                    key--;
                }
                //again make the result back to 1 for next key element
                result=new BigInteger("1");
            }
        }catch(Exception e)
        {
            //do nothing
        }
    }
}

Updated on 05-Oct-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By