JAVA large Number Class

Source: Internet
Author: User

Encounter a lot of large number of topics, heard Java comparison Rogue, hurriedly to learn Ipo (<?????) >)

Add large numbers:

1 //Add a large number of JAVA2 import Java.math.BigInteger;3Import java.util.*;4Import java.io.*;5 6  Public classMain7 {8      Public Static voidMain (String args[])9     {TenScanner cin =NewScanner (System.inch); OneBigInteger A =Cin.nextbiginteger (); ABigInteger B =Cin.nextbiginteger (); -BigInteger ans =A.add (b); -System. out. println (ans); the     } -}

Multiply by large number:

1 //Multiply JAVA Large numbers2 import Java.math.BigInteger;3Import java.util.*;4Import java.io.*;5 6  Public classMain7 {8      Public Static voidMain (String args[])9     {TenScanner cin =NewScanner (System.inch); OneBigInteger A =Cin.nextbiginteger (); ABigInteger B =Cin.nextbiginteger (); -BigInteger ans =a.multiply (b); -System. out. println (ans); the     } -}

Subtraction of large numbers: public BigInteger subtract (BigInteger val)

Divide large number: public BigInteger divide (BigInteger val)

Large number factorial:

1 //JAVA large number factorial2Import java.io.*;3 import Java.math.BigInteger;4Import java.util.*;5 6  Public classMain7 {8      Public Static voidMain (String args[])9     {TenScanner cin =NewScanner (System.inch);  One         intn = cin.nextint ();//Enter int type ABigInteger ans =Biginteger.one; -          for(inti =1; I <= N; i++) -Ans =ans.multiply (biginteger.valueof (i)); theSystem. out. println (ans); -     } -}

Large number comparison size

1 //JAVA Large number comparison size2Import java.io.*;3 import Java.math.BigInteger;4Import java.util.*;5 6  Public classMain7 {8      Public Static voidMain (String args[])9     {TenScanner cin =NewScanner (System.inch);  One          while(Cin.hasnext ()) A         { -BigInteger A =Cin.nextbiginteger (); -BigInteger B =Cin.nextbiginteger (); the             if(A.equals (Biginteger.zero) && b.equals (Biginteger.zero)) Break; -             intFlag =A.compareto (b); -             if(Flag = =-1) -System. out. println ("a<b"); +             Else if(Flag = =0) -System. out. println ("a==b"); +             Else ASystem. out. println ("a>b"); at         } -     } -}

High precision Power

1 //JAVA High-precision power2Import java.io.*;3 import Java.math.BigDecimal;4Import java.util.*;5 6  Public classMain7 {8      Public Static voidMain (String args[])9     {TenScanner cin =NewScanner (System.inch);  One          while(Cin.hasnext ()) A         { -BigDecimal ans =cin.nextbigdecimal (); -             intn =cin.nextint (); theString res = ANS.POW (n). Striptrailingzeros (). toplainstring ();//integer minus the 0 and decimal points in the back -             if(Res.startswith ("0"))//Remove leading 0 -             { -res = res.substring (1); +             } -System. out. println (res); +         } A     } at}

The BigDecimal here is equivalent to double, but with a higher precision

JAVA large Number Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.