---
title: "My Factorial program isn’t giving correct output using java"  
description: "My Factorial program isn’t giving correct output using java"  
author: "Anonymous User"  
published: 2013-10-05  
updated: 2013-10-05  
canonical: https://www.mindstick.com/forum/1623/my-factorial-program-isn-t-giving-correct-output-using-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# My Factorial program isn’t giving correct output using java

```
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
        }
    }
}
```

## Replies

### Reply by Dag Hammarskjold

result = result.multiply(BigInteger.valueOf(key));

Result should be printed, after perform operation in while loop. Look at modified code:

```
  key=Integer.parseInt(br.readLine());
  while(key>0){
       result = result.multiply(BigInteger.valueOf(key));
       key--;
  }
  System.out.println(key+" "+result);
```

## Note: -BigInteger is immutable. result need to reassign to get desired output like:


---

Original Source: https://www.mindstick.com/forum/1623/my-factorial-program-isn-t-giving-correct-output-using-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
