Home Java Beginners Add two big numbers
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Add two big numbers

Advertisement
In this section, you will learn how to add two big numbers.

Add two big numbers

     

In this section, you will learn how to add two big numbers. For adding two numbers implement two big decimal numbers then apply the Sum() method that takes both numbers. Now, you will have to define it and use the add() method to add both numbers. Finally, the result will be displayed on the command prompt (DOS prompt). 

BigDecimal():
This is the constructor of BigDecimal class. It translates the data into BigDecimal. 

BigDecimal.add(BigDecimal big):
This method is used to add the two BigDecimal numbers.

Here is the code of program:

import java.math.BigDecimal;

public class AddTwoBigNumbers{
  public static void main(String[] args) {
  BigDecimal num1, num2;
  num1 = new BigDecimal(50.00035);
  num2 = new BigDecimal(100.0025);
  Sum(num1, num2);
  }

  public static void Sum(BigDecimal val1, BigDecimal val2){
  BigDecimal sum = val1.add(val2);
  System.out.println("Sum of two BigDecimal numbers: "+ sum);
  }
}

Download this example.

Output of program:

C:\vinod\Math_package>javac AddTwoBigNumbers.java

C:\vinod\Math_package>java AddTwoBigNumbers
Sum of two BigDecimal numbers: 150.002849999999995134203345514833927154
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 11, 2007

Ask Questions?    Discuss: Add two big numbers   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Angel Vaughn
September 2, 2011
message

I lOVE iT So MUch!!!!!!!!!!!!
151mewtwo
December 8, 2011
Add Two Big Numbers

hi can you tell me how to add a number larger than 1 billion? i need to make a program adding two very large numbers that doesn't normally fit in the calculator..
151mewtwo
December 8, 2011
Add Two Big Numbers

hi can you tell me how to add a number larger than 1 billion? i need to make a program adding two very large numbers that doesn't normally fit in the calculator..
DMCA.com