Thursday, 17 January 2013

Addition of Two Numbers Java Programming Code

Java program to add two numbers

Simple Program to Add two numbers in Java



//A Simple Program to Add Two Numbers in Java Program.



class AddTwoNumbers        //Class Declaration
{
   public static void main(String args[]) //Main Function
   {
      int x, y, z;      // Variable Declaration
      x = 10;           //Assigning 10 to the variable x.
      y = 20;           //Assigning 20 to the variable y.
      z = x + y;        //Expression to Add two variables x, y and save the result to z
      System.out.println("Sum of x and y = "+z); //This line output the value of z on the Screen
   }
}


Output:

Sum of x and y =30

39 comments :

  1. y = 20; //Assigning 10 to the variable y.
    Place of Above Line
    y = 20; //Assigning 20 to the variable y

    ReplyDelete
  2. y = 20; //Assigning 20 to the variable y
    place of above line

    ReplyDelete
  3. very good, learn more...............

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. I AM FROM ANONYMOUS NOOB CODES

    ReplyDelete
  6. but i want take two numbers by user,
    how to take numbers by user and then calculate it's sum
    please tell me sir
    thanks.

    ReplyDelete
    Replies
    1. http://www.javatutorialprograms.com/2013/01/java-program-to-add-two-numbers.html

      Delete
    2. import java.io.*;
      class sid
      {
      public static void main(String [] args)throws IOException
      {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter two no.");
      int x=Integer.parseInt(br.readLine());
      int y=Integer.parseInt(br.readLine());
      int z=x*y;
      int a=x+y;
      System.out.println("Siddhant"+z+a);
      }
      }





      //Sorry cant explain how it is but this is is try it

      Delete
    3. import java.util.Scanner;
      public class Addition {

      public static void main(String[] args) {
      // TODO Auto-generated method stub

      int a,b,c;
      System.out.println("Enter the two numbers here");

      Scanner me = new Scanner(System.in);

      a= me.nextInt();
      b=me.nextInt();
      c=a+b;
      System.out.println("Addition of two values= " +c);


      }

      }

      [this one is simpler and easy]

      Delete
  7. Post is good for absolute noobs. Check this out : Short article on how to learn java programming

    ReplyDelete
  8. Post is good for absolute noobs. Check this out : Short article on how to learn java programming Java programming tutorial

    ReplyDelete
  9. Instead of buffer readers u can also take scanner class.

    ReplyDelete
  10. import java.util.Scanner;

    class AddNumbers
    {
    public static void main(String args[])
    {
    int x, y, z;
    System.out.println("Enter two integers to calculate their sum ");
    Scanner in = new Scanner(System.in);
    x = in.nextInt();
    y = in.nextInt();
    z = x + y;
    System.out.println("Sum of entered integers = "+z);
    }
    }

    ReplyDelete
    Replies
    1. im running this on Mac im getting 11 errors

      Delete
  11. Post is good..
    How about printing like this :

    int x=10,y=20;
    System.out.println("sum : "+(x+y));

    See this as well : Best way to learn java programming

    ReplyDelete
  12. how short datatype use in addition method

    ReplyDelete
  13. how short datatype use in addition method

    ReplyDelete
  14. sir ,,,can u tell me how to run java program using cmd

    ReplyDelete
    Replies
    1. first
      javac class name.java

      then
      java class name

      Delete
    2. This comment has been removed by the author.

      Delete
    3. kindly visit the link: http://java191.blogspot.com.ng/2017/02/how-to-compile-and-execute-java-program.html

      Delete
  15. import java.util.Scanner;

    class AddNumbers
    {
    public static void main(String args[])
    {
    int x, y, z;
    System.out.println("Enter two integers to calculate their sum ");
    Scanner in = new Scanner(System.in);
    x = in.nextInt();
    y = in.nextInt();
    z = x + y;
    System.out.println("Sum of entered integers = "+z);
    }
    }

    ReplyDelete
    Replies
    1. hi, im running this same code on mac and I am getting 11 errors

      Delete
  16. if i want to declare variables with in class then how to write a program for that

    ReplyDelete
    Replies
    1. You can just add public static int x, y, z; in the class to use the variables. Java program to add two numbers

      Delete
  17. if i want to declare variables with in class then how to write a program for that

    ReplyDelete
  18. Write A Programs
    Good Explanation.....
    Helpful For Beginners.

    ReplyDelete
  19. in addition to the program. We can modify the program in such a way that the user input two numbers then it displays the sum of the two numbers in a message dialog box. You can check out the program and detailed explanation of the code through the following link: http://java191.blogspot.com.ng/2017/02/java-addition-program-that-uses.html

    ReplyDelete
  20. Good and nice information, thanks for sharing your views and ideas...
    Web Designing Training in chennai | PHP Training in chennai

    ReplyDelete
  21. Data type not mention....It generate error

    ReplyDelete