Ticker

6/recent/ticker-posts

Header Ads Widget

Java program to add/subtract/divide/multiply two numbers

Java program to add/subtract/divide/multiply two numbers


Introduction

As clear from the heading we are now gonna learn how to add/subtract/divide/multiply two numbers using a Java program.

Before start let me give you you are clear idea about what we gonna do in this we take two number as a input from the user and then apply mathematical operation on it one by one add, subtract, divide, multiply.

So let's get started!!!!

As as there is only difference is of sign so we we do this all operation in a single program so that you can easily understand the difference and the similarities between all the functions very well.

Q) write a Java program to add two numbers.
Q) write a Java program to subtract two numbers.
Q) write a Java program to divide two numbers.
Q) write a Java program to to multiply two numbers.
Q) write a Java program to add subtract multiply and divide two numbers.


Now we gonna do all these above questions.
First let see the code.

Example 1) here we perform mathematical operation on already defined variable.

Code :




public class Main {
public static void main(String[] args) {
int a,b,c,d,x=14,y=7;
a= x+y ;
b=x-y;
c=x*y;
d=x/y;
System.out.print(a +"\n");
System.out.print(b + "\n");
System.out.print(c +"\n");
System.out.print(d);
}
}






Let analyse the above code for the difference before that you might be wondering of "\n" what is this in the code doing.so it basically tell compiler two have a line break so that the all output will be in in different lines. Now what we see here in this code is that firstly we declare different variables play a bc and x y.
x and y for defining some intvalue and other  a,b,c,d to store the value of different operations after that we printer result using are ordinary method there is one new thing in that we use here line break .

Now here question arise what is line break let define it first and move further.

So come to the output of the above code.

Output:



21
7
98
2



If we don't use line break here the output will be like this,
217982
And it will be hard to understand the output,so now you will be clear that how important line break is.






Q) What  is line break in Java and what its use?

Ans) line break is is defined bus symbol "\n" .it is used to break a line so that different output can be shown in different lines so that they cannot make seen one another and make it hard for user to read it this also used in many  languages like c ,c + + ....etc.


Example 2) now we create a program that will take a a user input and store it in a variable and perform mathematical operation on it

Q) write a Java program to take input of two number and perform mathematical operation on it like add subtract multiply divide.

Sol.







import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a,b,c,d,x,y;
System.out.print("Enter the first number \n");  x = in.nextInt();
System.out.print("Enter the second number \n");
 y = in.nextInt();
a= x+y ;
b=x-y;
c=x*y;
d=x/y;
System.out.print(a +"\n");
System.out.print(b + "\n");
System.out.print(c +"\n");
System.out.print(d);
}
}







So this is the code to perform the various mathematical operation on two number after getting input of two number from the user and then show it on the screen as output.

Let see how would be the output see like.

Output:

Enter the first number
8
Enter the second number
2
10
6
16
4



So, now I hope you understand the program. If there is any doubt you can comment as below in case you face any problem.

Post a Comment

0 Comments