Program to Add Two Numbers
Hello friends, in this tutorial we are going to write a program to add two numbers by getting input from the user. So, open your IDE and make a new Project. Now, write the following code in Main.java file.
Main.java :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int x;
int y;
int sum;
System.out.println("Enter the first number : ");
Scanner scanner = new Scanner(System.in);
x = scanner.nextInt();
System.out.println("Enter the second number : ");
y = scanner.nextInt();
sum = x + y;
System.out.println("Sum of the given numbers is : " + sum);
}
}
public class Main {
public static void main(String[] args) {
int x;
int y;
int sum;
System.out.println("Enter the first number : ");
Scanner scanner = new Scanner(System.in);
x = scanner.nextInt();
System.out.println("Enter the second number : ");
y = scanner.nextInt();
sum = x + y;
System.out.println("Sum of the given numbers is : " + sum);
}
}
Your output will be like this.
Output :
C:/Users/Username/Desktop>Enter the first number :
50
Enter the second number :
50
Sum of the given numbers is : 100
50
Enter the second number :
50
Sum of the given numbers is : 100