Wednesday, 30 January 2013

Write a java program to add two numbers

This java program will illustrate the addition of two integer numbers. This operation is an important aspect for programmer where this logic is going to apply for several applications. Keeping those things in mind we start the process of addition using java program. 

The steps involved are :

we take two input from the user and using traditional " + " operator we add both the integer. As numbers are of the type "int" so we get the addition of both number as a result. 



import java.util.Scanner;  
 public class Add   
 {  
   public static void main(String[] args)   
   {    
     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.printf( "Enter first integer: " ); // prompt  
     number1 = input.nextInt(); // read first number from user  
     System.out.printf( "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      
   }  
 }  

No comments:
Write comments

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2016