Program to add two integer numbers in Java

First :

package com.company;

import java.util.Scanner;
public class Main {

public static void main(String[] args) {

int number1, number2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your first number to add:");
number1 = sc.nextInt();

System.out.println("Enter your second number to add:");
number2 = sc.nextInt();

sc.close();
sum = number1 + number2;
System.out.println("The sum of these two numbers will be:"+sum);
}
}

 

Output:

Second :

package com.company;

public class Main {

public static void main(String[] args) {

int number1 = 21;
int number2 = 11;

System.out.println("The two numbers to be added are: " + number1 + " " + number2);

int add = number1 + number2;

System.out.println("The addition of these two numbers is: " + add);
}
}

Output :

Third :

package com.company;
public class Main {

public static void main(String[] args) {

int number1 = 23;
int number2 = 17;
int add = number1 + number2;

System.out.println(add);

}
}

Output :

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments