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-
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
//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
import java.io.*;
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));
}
}
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
great codes
ReplyDeleteAmazing!! Nice explanation with examples.. Thanks for sharing.
ReplyDeleteCheers,
http://www.flowerbrackets.com/area-circle-calculate-circumference/