Java program to add two numbers.docx - Java program to add two numbers Java program to add two numbers A user will enter two integers and we will

Java program to add two numbers.docx - Java program to add...

This preview shows page 1 - 5 out of 12 pages.

Java program to add two numbers Java program to add two numbers: A user will enter two integers and we will calculate their sum and then display it. First program can only add numbers which can be stored in 4 bytes (size of integer data type), if you wish to add very large numbers, then you can use BigInteger class (see second program). Java programming code 1. import java.util.Scanner ; 2. 3. class AddNumbers 4. { 5. public static void main ( String args []) 6. { 7. int x, y, z ; 8. 9. System . out . println ( "Enter two integers to calculate their sum" ) ; 10. Scanner in = new Scanner ( System . in ) ; 11. 12. x = in. nextInt () ; 13. y = in. nextInt () ; 14. z = x + y ; 15. 16. System . out . println ( "Sum of the integers = " + z ) ; 17. } 18. }
Image of page 1
Output of program: Java program to add large numbers using BigInteger class 1. import java.util.Scanner ; 2. import java.math.BigInteger ; 3. 4. class AddingLargeNumbers { 5. public static void main ( String [] args ) { 6. String number1, number2 ; 7. Scanner in = new Scanner ( System . in ) ; 8. 9. System . out . println ( "Enter first large number" ) ; 10. number1 = in. nextLine () ; 11. 12. System . out . println ( "Enter second large number" ) ; 13. number2 = in. nextLine () ; 14. 15. BigInteger first = new BigInteger ( number1 ) ; 16. BigInteger second = new BigInteger ( number2 ) ; 17. BigInteger sum ; 18. 19. sum = first. add ( second ) ; 20. 21. System . out . println ( "Result of addition = " + sum ) ; 22. }
Image of page 2
23. } Output of program: 1. Enter first large number 2. 11111111111111 3. Enter second large number 4. 99999999999999 5. Result of addition = 111111111111110 In our code, we create two objects of BigInteger class of java.math package. Input should be digit strings otherwise an exception will be thrown; also you cannot just use '+' operator to add objects of BigInteger class, you have to use the add method for addition of two objects. - code/java-program-add-two-numbers For more information and to be able to download the two codes for free go to the web link above.
Image of page 3
- subtraction-multiplication-division.htm
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 12 pages?

  • Spring '19

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors