import java.util.Scanner;
public class Add
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter 2 numbers : ");
int x = scan.nextInt();
int y = scan.nextInt();
int sum = x+y;
System.out.println("Sum of " + x + " and " + y + " is : " + sum);
}
}
Output:
Enter 2 numbers :
10
20
Sum of 10 and 20 is : 30
Adding using method:
import java.util.Scanner;
public class Add
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter 2 numbers : ");
int x = scan.nextInt();
int y = scan.nextInt();
int sum = Add.calc(x,y);
System.out.println("Sum of " + x + " and " + y + " is : " + sum);
}
static int calc(int x, int y)
{
return x+y;
}
}
Output:
Enter 2 numbers :
10
20
Sum of 10 and 20 is : 30