add two numbers

Java Program to Add Two Numbers with Example

In this section, we will look at two programs that add two numbers. In the first program, we specify the values of both numbers in the program itself. The second program adds both numbers (entered by the user) and prints the total.

Java Program to Add Two Numbers with Example

Example: First

public class SumTwoNumbers {

   public static void main(String[] args) {
        
      int num1 = 5, num2 = 15, sum;
      sum = num1 + num2;

      System.out.println("Sum of these numbers: "+sum);
   }
}

Output:

Sum of these numbers: 20

Example: Second

The scanner allows us to capture user input in order to obtain the values of both numbers from the user. The program then computes and displays the total.

public class SumTwoNumbers2 {

    public static void main(String[] args) {
        
        int num1, num2, sum;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter First Number: ");
        num1 = sc.nextInt();
        
        System.out.println("Enter Second Number: ");
        num2 = sc.nextInt();
        
        sc.close();
        sum = num1 + num2;
        System.out.println("Sum of these numbers: "+sum);
    }
}

Output:

Enter First Number:
121
Enter Second Number:
19
Sum of these numbers: 140

Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today.

lSimple Example of Java

Add two number,Add two number,Add two number,Add two number

Leave a Reply