add two numbers in java code example
Example 1: java sum of two numbers program intellij
public class AddTwoNumbers {
public static void main(String[] args) {
int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}
Example 2: java program to add two numbers using method
import java.util.Scanner;
public class AddTwoNumbers
{
public static void main(String[] args)
{
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("First number : ");
num1 = sc.nextInt();
System.out.println("Second number : ");
num2 = sc.nextInt();
sum = addTwo(num1, num2);
System.out.println("Sum : " + sum);
sc.close();
}
public static int addTwo(int a, int b)
{
int sum = a + b;
return sum;
}
}
Example 3: add two numbers in java
public class AddTwoNumber
{
public static void main(String[] args)
{
int number1 = 12;
int number2 = 3;
int sum = number1 + number2;
System.out.println("Sum of "+number1+" and "+"number2 is : "+sum);
}
}