F Program To Add Two Numbers In JAVA Using Predefined And User Input Values. | CodeTheta

Program To Add Two Numbers In JAVA Using Predefined And User Input Values.

August 04, 2016

With predefined values
Code:
class Addnum {
  public static void main (String args[])
  {
    int FirstNumber=1,SecondNumber=1,Result;
    Result=FirstNumber+SecondNumber;
    System.out.println("Result of two numbers: "+Result);
  }
}

Output : 
Result of two numbers: 2

With user input values
Code :
import java.util.Scanner;
class Add_num_by_user_input {
  public static void main (String args[])
  {
    int FirstNumber,SecondNumber,Result;
    System.out.println("Enter two number for addition: ");
    Scanner in = new Scanner(System.in);
    FirstNumber = in.nextInt();
    SecondNumber = in.nextInt();
    Result=FirstNumber+SecondNumber;
    System.out.println("Result is: "+Result);
  }
}

Output : 
Enter two number for addition:
3
4
Result is: 7

IDE Used To Test This Code : Dr Java

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

Post a Comment