java - n! BigInteger makes a loop -
I wrote this small program to search n!:
public class Fattoriale {public static zero main (string [] args) {int n; Do {n = input. ReadInt ("Enter a whole number (int> = 0)"); While (N & T; 0); Tall fat = 1; For (int i = 2; i & lt; = n; i ++) {fatta = fatt * i; } System.out.println (n "+!" + "=" + Fatt);
Now I am trying to rewrite this program using BigInteger. I have written this:
Import jbook.util. *; Import java.math. *; Public class Fattoriale {public static zero main (string [] args) {string s = input. ReadString ("Enter an integer number."); BigInteger big = new BigIntega (S); BigInteger tot = new bigigner ("1"); BigIntega i = New BigInteger ("2"); (; I.compareTo (big) & lt; 0; i.add (BigInteger.ONE)) for {tot = tot.multiply (i); } System.out.println (tot); }}
But with BigInteger this program creates a loop and why I can not understand I hope someone will help me. Thanks a lot ;). (Ignoring the NB input square, this is just a class that I have created to enter input)
This should be done as it does not update i.add (BigInteger.ONE)
variable i
.
For (; i.compareTo) (big)
There are two changes:
- This is
and <0> <0> instead of
should be - Assign it to
i
to return it.
How to confirm?
BigInterizer i = New BigIntegr ("2"); Println (i.add (BigInteger.ONE)); // print 3 System.out.println (i); // print2
Comments
Post a Comment