Calculations

Addition of two numbers in Java using method

addition of two numbers in Java using methods

In this tutorial, we will discuss the addition of two numbers in Java using the Java method.

In this topic, we will learn a simple concept of how to add two number in Java programming language using the Java method.

already we will know the same concept using the operator in a simple way.

if you known click here Java program to sum of two numbers

This program has following procedures to completion

  • class and main method
  • get input from user – using Scanner class
  • method definition – user-define method
  • calling the method
  • Display method on the screen

 

Calculate the total value of the two integer number

Calculate the total value of two integer number using the method

import java.util.Scanner;
public class SumOfNum{

public static void main(String args[]){

Scanner sc=new Scanner(System.in);
// create scanner object

System.out.print("Enter the first number: ");

int num1=sc.nextInt();
//this method reads value for num1 providing by user

System.out.print("Enter the second number: ");

int num2=sc.nextInt();
//this method reads value for num2 providing by user

sc.close();//closing the scanner class

sumNum(num1,num2); //calling the method
}
//sum method
public static void sumNum(int num_A,int num_B){
int sum=0;
sum=num_A+num_B;
System.out.println("Sum of two numbers  "+sum);

}
}

When the above code is compiled and executed, it produces the following results

Enter the first number: 45 
Enter the second number: 32 
Sum of two numbers 77

 

Program 2

Calculate total value of two integer number using the method with the return value

import java.util.Scanner;
public class SumOfNumbur{

public static void main(String args[]){

Scanner sc=new Scanner(System.in);
// create scanner object

System.out.print("Enter the first number: ");

int num1=sc.nextInt();
//this method reads value for num1 providing by user

System.out.print("Enter the second number: ");

int num2=sc.nextInt();
//this method reads value for num2 providing by user

sc.close();//closing the scanner class

int Total=calcTotal(num1,num2); //calling the method
System.out.println("Sum of two numbers  "+Total);
}
//calcTotal method
public static int calcTotal(int x,int y){
int result=x+y;
return result;//return result to main


}
}

When the above code is compiled and executed, it produces the following results

Enter the first number: 55
Enter the second number: 65
Sum of two numbers  120

In this program, we can denoted some impotent steps to completed this program

  • class and main method
  • get input from user – using Scanner class
  • method definition – user-define method
  • calling the method
  • return value to main method

 

Calculate total value of two floating point number

Calculate the total value of the two floating point number using the method

import java.util.Scanner;
public class SumOfNum1{

public static void main(String args[]){

Scanner sc=new Scanner(System.in);
// create scanner object

System.out.print("Enter the first number: ");

double num1=sc.nextDouble();
//this method reads value for num1 providing by user

System.out.print("Enter the second number: ");

double num2=sc.nextDouble();
//this method reads value for num2 providing by user

sc.close();//closing the scanner class

calcTotal(num1,num2); //calling the method

}
//calcTotal method
public static void calcTotal(double num_A,double num_B){
double result;
result=num_A+num_B;
System.out.println("Sum of two numbers  "+result);

}
}

When the above code is compiled and executed, it produces the following results

Enter the first number: 34.23
Enter the second number: 32.54
Sum of two numbers  66.77

 

 

 

Similar post

Addition of two numbers in Java

Addition of two numbers in C

Addition of two numbers in C++

Addition of two numbers in Python

Addition of two numbers in C#

Addition of two numbers in PHP

Addition of two numbers in JavaScript

 

Sum of two numbers in Java using method

Sum of two numbers in C++ using function

Sum of two numbers in Python using function

 

C program to add two numbers without using arithmetic operators

C++ program to add two numbers without using arithmetic operators

Python program to add two numbers without using arithmetic operators

 

Suggested for you

For loop in Java language

While loop in Java language

The method in Java language

Operator in Java language

variable in Java language

Data type of Java

 

C program to add two numbers using function
Cpp program to add two numbers using function
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

  • I want to add 3 double numbers for example : 4.67, 8.93, 10.00. if result is natural number return 1 else return 0. what is the condition? can you help me?

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago