Write a program in java to subtract two integer numbers by using scanner class.
Answers
Answer:
I hope it will help you if you like this mark brainlest I have given you extra points
Explanation:
Java program for Addition, Subtraction, Multiplication and Division. Here we will discuss the most common mathematical operations such as addition, subtraction, multiplication and division In java. The compiler has been added as well so that you can execute the programs yourself, along with suitable examples and sample outputs. The programs as aforementioned are:
Addition.
Addition using Static Method.
Subtraction.
Multiplication.
Multiplication without using the Multiplication(*) operator.
Division.
Division without using the Division (/) operator.
Java Program – Addition
1) We are using the standard formula for adding two numbers.c=a+b.
Left Arrow Star Pattern Java Program – Patterns
30+ Number & Star Pattern Programs In Java – Patterns
2) Read the values using scanner object sc.nextInt() and store these values in the variables a,b and calculate addition of a,b and print the c value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner;
class Add
{
public static void main(String[] arg)
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.println("Enter first number");
a=sc.nextInt();
System.out.println("Enter second number");
b=sc.nextInt();
int c=a+b;
System.out.println("Addition of two numbers is : "+c);
}
}
Addition(Using Static Method)
1) addition(int x, int y) is the static method, which calculates the addition of two numbers and returns the value.
2) addition(a,b) method calls at the main method then that static method calculates the addition of two numbers and returns the value and value will be assigned to the variable c.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
class Add
{
public static void main(String[] arg)
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter first number");
a=sc.nextInt();
System.out.println("Enter second number");
b=sc.nextInt();
c=addition(a,b);
System.out.println("Addition of two numbers is : "+c);
}
static int addition(int x,int y)
{
return x+y;
}
}
Subtraction Java Program
1) We are using the formula for subtraction is c=a-b.
2) Read the values using scanner object sc.nextInt() and store these values in the variables a,b. Subtract the smaller value from bigger value and result will be assigned to c and print the c value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;
class Sub
{
public static void main(String[] arg)
{
int a,b,c;
Scanner s=new Scanner(System.in);
System.out.println("Enter first number");
a=s.nextInt();
System.out.println("Enter second number");
b=s.nextInt();
if(a>b)
c=a-b;
else
c=b-a;
System.out.println("Subtraction of two numbers is : "+c);
}
}
Java Multiplication Program
1) The formula for multiplication of two numbers is c=a*b.
2) Read the values using scanner object sc.nextInt() and store these values in the variables x,y and calculate multiplication of these numbers then print the z value.