Google Add

Search

Program to Add Two Numbers in Java

Write a java program to add two numbers. This is very basic java program mainly for beginners who are just started coding in java.

Program to add two number in C, C++

Java Programs

How to solve this problem

i) Take two input numbers from user.

ii) Add these two numbers and print their output.

Program to Add Two Numbers in Java


import java.util.*;

public class Addnumbers {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        int first, second, sum;
        
        System.out.println("Enter two numbers to calculate their sum \n");
        
        Scanner in = new Scanner(System.in);

        /* Taking inputs */

        first = in.nextInt();
        second = in.nextInt();
        
        /* Add two numbers */

        sum = first + second;
        
        System.out.println("The sum of two numbers is "+ sum);
        
    }
    
}



Output :

Enter two numbers to calculate their sum :  8    6

The sum of two numbers is 14

No comments:

Post a Comment