Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-28-2008, 09:46 PM
Java Tip's Avatar
Moderator
 
Join Date: Nov 2007
Posts: 1,691
Rep Power: 5
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Default Addition program that displays the sum of two numbers
The Java program below reads two integers typed by a user at the keyboard, computes the sum of the values and displays the result.

Code:
import java.util.Scanner;

public class Addition
{
    // main method begins execution of Java application
    public static void main( String args[] )
    {
       // create Scanner to obtain input from command window
       Scanner input = new Scanner( System.in );            

       int number1; // first number to add   
       int number2; // second number to add  
       int sum; // sum of number1 and number2

       System.out.print( "Enter first integer: " ); // prompt
       number1 = input.nextInt(); // read first number from user

       System.out.print( "Enter second integer: " ); // prompt
       number2 = input.nextInt(); // read second number from user

       sum = number1 + number2; // add numbers
       System.out.printf( "Sum is %d\n", sum ); // display sum

    } // end method main

 } // end class Addition
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
jTextField Simpe Addition - how to ? - Netbeans 6.0 jesicapalma NetBeans 2 03-25-2008 12:07 PM
Varargs – Java 5.0 addition JavaForums Java Blogs 0 11-13-2007 05:40 PM
Write an application that displays the numbers 1 to 4 on the same line toby New To Java 1 07-23-2007 05:33 PM
Problem when displays in the terminal Felissa Advanced Java 2 07-01-2007 03:14 AM


All times are GMT +2. The time now is 09:46 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org