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

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

Operator in Java

Data type of Java

Method in Java language

 

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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago