Sum of Two Numbers in Java

Download
Download is available until [expire_date]
  • Version
  • Download 19
  • File Size 2.55 KB
  • File Count 1
  • Create Date April 18, 2016
  • Last Updated April 18, 2016

Sum of Two Numbers in Java

Sum of Two Numbers in Java

This is an example of java program that accepts inputs (first number and second number) from the user and computes the sum of two numbers. It’s coded in two versions; the console and gui using swing library.

Source code:

import javax.swing.*;
public class SumofTwoGUI
{ 
public static void main(String args[]) {

String input1, input2;	 
double firstnum ; 
double secondnum ;
double result;

input1 = JOptionPane.showInputDialog(null,"Enter first number: ");
input2 = JOptionPane.showInputDialog(null,"Enter second number: ");

firstnum=Double.parseDouble(input1);
secondnum=Double.parseDouble(input2);

result=firstnum+secondnum;
JOptionPane.showMessageDialog(null,"Sum: " + result );

	}
}
, , , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.