about-text

SoftOSet is the most remarkable programming site. SoftOSet provides the fresh conceptual and professional programs, articles, books, and other interesting and technical writings. SoftOSet is the most reliable programming site to learn the programs and the basics of the popular programming languages. SoftOSet is the hub of the informative and research based articles.


 

Welcome my Java fellows!

 


In this blog we are going to develop a program in java language which will add two numbers. There are different ways in java to add two numbers. We will explore only three types to add two numbers in java language.

 

Method 1: Adding By Direct Static Method:

 

In this method, we will add the two numbers directly using the static program. So let’s we want to add the 40 and 100. We can add these numbers by using this code.

 

Code:

public class Main{
    public static void main(String[] args)
    {
        System.out.println(40+60);
    }
}

 

Output:

 

Here is the output of this program when we compile it using the InteliJ Idea IDE.

 


 

 
Method 2: Adding By Variable Static Method:

 

Adding by Variable static method is same like as adding by direct static method. The only difference is the in variable static method. We will store our numbers in the variables. Then perform addition operation and store the result in another variable and than print this result on screen. This can be achieved by using following program.

 

Code:

 

public class Main{
    public static void main(String[] args)
    {
        int num1=40;
        int num2=50;
        int result;
        result=num1+num2;
        System.out.println(result);
    }
}

 

Output:

This program of addition has the following output.

 



 
Method 3: Addition by taking the values from user at run time:

 

In addition by taking the values from user at run time our program will ask the user to enter the numbers of their own choice which they want to add. The program will prompt the user to enter first number and then second number. Add these numbers and store the result in a third variable and display the result. The program segment is as follow.

 

Code:

 

import java.util.Scanner;
public class Main{
    public static void main(String[] args)
    {
       int num1;
       int num2;
       int result;
        System.out.println("Welcome in Addition Program");
        Scanner input=new Scanner(System.in);
        System.out.println("Enter First Number :");
        num1=input.nextInt();
        System.out.println("Enter 2nd Number :");
        num2=input.nextInt();

        result=num1+num2;
        System.out.println("Addittion of two numvbers ="+result);

    }
}

 

Output:

 You can see the running of this program in the below snapshot.



 

 

 

 

 

 

 

 

Hopefully you have grab all the concepts related to the addition of variables in Java. If something is missing or you want to improve this article, Please comment us below with your precious suggestions.

Post a Comment

Feel Free to Comment Us

Previous Post Next Post

Recents