Java Program to Add Two Numbers using different methods(methods in different ways)

Simple Java Program to Add Two Numbers

This simple Java program let the user to enter two integer values and then add those two numbers and assign it to variable sum.
JAVA CODE:
//Addition of two numbers by taking input from user.
import java.io.*;
import java.util.*;

class Add_Two_Num_input{
    public static void main(String args[]){
        int num1,num2,sum;
        System.out.println(“Enter two numbers “);
        Scanner input= new Scanner(System.in);
        num1=input.nextInt();
        num2=input.nextInt();
        sum=num1+num2;
        System.out.println(“The sum of the number is “+sum);
       
    }
}

OUT-

Enter two numbers
78
8
The sum of the number is 86


Java Program to Add Two Numbers using Methods

import java.io.*;

import java.util.*;


class AdditionOops
{
    
    public static void main(String args[])
    {  
int num1,num2;
       System.out.println(“Enter two numbers”);
       Scanner input = new Scanner(System.in);
       num1=input.nextInt();
       num2=input.nextInt();
       Add object = new Add();
      System.out.println(“sum is” +object.addition(num1,num2));
   }
//here we used static keyword so that we dont need to create an object for addition method
   static int addition(int num1,int num2)
      {
        int sum;
        sum=num1+num2;
       return sum;
}
}   
        

Java Program to Add Two Numbers using OOPs

(In OOPs we create two classes in first class we define fields or variables and methods and in main class that is in second class we call methods and fields using object)

//Addtion of two numbers using method addition passing arguments as num1,num2.
//Addtion of two numbers using method addition passing arguments as num1,num2.

import java.io.*;
import java.util.*;



 class add
{

int   addition(int num1,int num2)
{             int num3;         
  num3=num1+num2;
      return num3;
  
        }
}
 class Add
{
public static void main(String args[])
        {
int num1,num2,num3;
Scanner input = new Scanner(System.in);
add obj = new add();

System.out.println(“Enter two numbers”);
num1=input.nextInt();
num2=input.nextInt();

System.out.println(“Sum is “+obj.addition(num1,num2));



 
   }
}
        

OUTPUT-
Enter two numbers
2
5
Sum is 7

Java program for addition of two numbers using constructor

//Addition of two numbers using parameterized constructor

import java.io.*;

import java.util.*;
class Add
{   
//constructor for addition
    Add(int num1,int num2)
{
int sum;
sum=num1+num2;
System.out.println(“Addition is” +sum);
}
}
class Addition_constr
{
public static void main(String args[])
{
Add object = new Add(3,5);
}
}
output -Addition is8

// Addition of two numbers using constructor and a method Addition.

import java.io.*;
import java.util.*;
class Add
{
int num1,num2;
Add(int x,int y)
{
   num1=x;
   num2=y;
}
int Addition()
{  
int sum;
sum=num1+num2;
return sum;
    }
}
class Addition_constructor_method
{
public static void main(String args[])
{
Add object = new Add(15,16);
System.out.println(“sum is ” +object.Addition());
    }
}
OUTPUT-sum is 31

Leave a Comment

Your email address will not be published. Required fields are marked *

Hire a server Expert to resolve the issue Now.

Hire a server Expert to resolve the issue Now.

Resolve this issue in just 5$ from https://serverexpert.io

 

You have Successfully Subscribed!

Open chat