Add two numbers program in Java (with and without user input)

public class AddTwoNumbers{
	public static void main(String[] args) {
		int a = 10;
		int b = 20;
		System.out.print(a+b);
	}
}
public class AddTwoNumbers{
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int sum = a+b;
        System.out.print(sum);
    }
}
import java.util.Scanner; // importing Scanner Package
public class AddTwoNumbers{
    public static void main(String[] args) {
        int a; // declaration for variable
        int b; //declaration for variable
        int sum; //declaration for variable
        Scanner in = new Scanner(System.in); //creating Scanner class object
        System.out.println("Enter the first number: ");
        a = in.nextInt(); // storing first number in veriable 'a'
        System.out.println("Enter the second number: ");
        b= in.nextInt(); // storing second number in veriable 'b'
        sum = a+b; // formula for performing addition
        System.out.print("The addition of two numbers is: "+sum);
    }
}

Leave a Reply