Java Program to find sum of two integer numbers
In this program let us create a Java program to print the sum of two numbers on the console.
Program
public class AddNumbers {
public static void main(String args[]) {
int a, b;
a = 10;
b = 32;
int sum = a + b;
System.out.println("Sum of " + a + " and " + b + ":\t" + sum);
}
}
Let us understand the program by breaking into simple parts:
public class AddNumbers
: Any Java program starts with a class, in this case the class name is AddNumberspublic static void main(String[] args)
: main is the entry point for any Java program.int a, b;
: Declare variables a and b of type integera = 10;
: Initiable variable a to 10b = 32;
: Initiable variable b to 32int sum = a + b;
: Calculate the sum of a and b and store it in variable sum.System.out.println("Sum of " + a + " and " + b + ":\t" + sum);
: System.out.println prints the value of a and b along with the sum.
Output
Sum of 10 and 32: 42