Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Add Two Numbers in Java

Add Two Numbers in Java

Sure! Here's a simple Java program to add two numbers:


Example 1: Add Two Numbers (Fixed Values)

public class AddTwoNumbers {    public static void main(String[] args) {        int num1 = 10;    // First number        int num2 = 20;    // Second number        int sum = num1 + num2;    // Adding the two numbers        System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);    }}

Output:

Sum of 10 and 20 is: 30

Example 2: Add Two Numbers (User Input)

If you want the user to input the numbers:

import java.util.Scanner;  // Import Scanner classpublic class AddTwoNumbersUserInput {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.print("Enter first number: ");        int num1 = scanner.nextInt();   // Read first number        System.out.print("Enter second number: ");        int num2 = scanner.nextInt();   // Read second number        int sum = num1 + num2;   // Adding the two numbers        System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);        scanner.close();    }}

Sample Output:

Enter first number: 15Enter second number: 25Sum of 15 and 25 is: 40

Would you also like me to show how to add two floating-point numbers (like decimals)? ?
It’s just as easy!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql