Java Addition through user input

How do you do addition in Java?

In this program, you’ll learn to store and add two integer numbers in Java. After addition, the final sum is displayed on the screen. To sum two number, we will use the mathematical plus (+) symbol between two numbers and assign into another variable. Example: c = a + b; We can perform addition between integer number to an integer number, float number to float number, etc or we can also do integer to float and float to double by type conversion. 

Java Addition through user input

Addition of two Integer Numbers

In this program, we will learn to do sum of two integer numbers. 

Java Program to Add two Integers Number Algorithm

  1. import java.io.*;
  2. import java.util.*;
  3. Create Class Class_Name
  4. public static void main(String[] args)
  5. Scanner ob = new Scanner (System.in);
  6. System.out.println( “enter value of x and y”);
  7. int x, y, sum;
  8. x = ob.nextInt();
  9. y = ob.nextInt();
  10. sum = x + y;
  11. System.out.println( ” Total sum is: “+sum);

Source code

import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner ob = new Scanner(System.in);
int x, y;
System.out.println("enter value of x");
x = ob.nextInt();
System.out.println("enter value of y");
y = ob.nextInt();
int sum = x + y;
System.out.println("Total sum is :" +sum);
}
}

Output:

enter value of x
10
enter value of y
20
Total sum is :30

Test Code Here:

Addition of Two Float Numbers in Java

In this program add two float numbers is not more different than addition two integer numbers. When we are writing a decimal value that will be considered as a double value so we have to do type conversion.

example:

a = 123.344 this value is considered as double ( double a = 123.344)

Float a = 123.344, this is a wrong way to declare float in java

Float b = 123.344f; this is considered as float value

Java Addition of two float numbers Algorithm

  1. import java.io.*;
  2. import java.util.*;
  3. Create Class Class_Name
  4. public static void main(String[] args)
  5. Scanner ob = new Scanner (System.in);
  6. System.out.println( “enter value of x and y”);
  7. Float x, y, sum;
  8. // Float x = 123.344f;
  9. // Float y = 121.344f;
  10. x = ob.nextFloat();
  11. y = ob.nextFloat();
  12. sum = x + y;
  13. System.out.println( ” Total sum is: “+sum);

Source Code:

import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner ob = new Scanner(System.in);
Float x, y;
System.out.println("enter value of x");
x = ob.nextFloat();
System.out.println("enter value of y");
y = ob.nextFloat();
Float sum = x + y;
System.out.println("Total sum is :" +sum);
}
}

Output:

enter value of x
123.334
enter value of y
121.344
Total sum is :244.67801

Test Code Here:

Addition of Two Numbers using sum() method

Addition program in java using sum() method user will enter the input two values with nextInt() method. The sum () method will take two arguments and return the addition of both arguments.

Algorithm:

  1. import java.util.*;
  2. Create Class Class_Name
  3. public static void main(String[] args)
  4. Scanner ob = new Scanner (System.in);
  5. System.out.println( “enter value of num1 and num2”);
  6. int num1, num2, num3;
  7. x = obs.nextInt();
  8. y = obs.nextInt();
  9. num3 = Sum(num1, num2);
  10. System.out.println( ” Total sum is: ” +num3);
  11. static int Sum(arg1, arg2)
  12. return art1 + arg2;

Source Code:

import java.util.*;
class Main
{
public static void main(String[] arg)
{
int num1, num2;
Scanner obs=new Scanner(System.in);
System.out.println("Enter first number");
num1 = obs.nextInt();
System.out.println("Enter second number");
num2 = obs.nextInt();
int num3 = Sum(num1,num2);
System.out.println(" Addition of two numbers is : "+num3);
}
static int Sum(int x,int y)
{
return x+y;
}
}

Output:

PS C:\Users\Pramod>  & 'c:\Users\Pramod\.vscode\extensions\vscjava.vscode-java-debug-0.31.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-15.0.2\bin\java.exe' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Pramod\AppData\Local\Temp\vscodesws_61e04\jdt_ws\jdt.ls-java-project\bin' 'Main' 
Enter first number
1
Enter second number
2
Addition of two numbers is : 3

Test Code Here:

Recommended Post:

Find the solution to the salesforce Question.

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment