The Java program given below is to perform several arithmetic operations. I have used six float type variables n1, n2, sum, sub, mul and div. n1 and n2 is used to store two data that will be used for several arithmetic operations. The program will not get any input from the keyboard, that's why the variables n1 and n2 are initialized by 7 and 3 respectively.
The next four 4 variables sum, sub, mul and div are initialized by 0. Later the line sum=n1+n2; is used to find out the sum of the numbers stored into the variables n1 and n2. Similarly, the line sub=n1-n2; is to find out the difference, the line mul=n1*n2; is to find out the product and the line div=n1/n2; is to find out the quotient. These four lines will perform the mathematical operations and will store the results into the variables sum, sub, mul and div.
Finally the stored result will be displayed by the next four lines. For example the line System.out.println("The Sum is="+sum); will display the result of addition stored into the variable sum.
Java program to Add, to Subtract, to Multiply and to Divide two numbers...public class ArithOp
{
public static void main(String [] args)
{
float n1=7;
float n2=3;
float sum=0;
float sub=0;
float mul=0;
float div=0;
sum=n1+n2;
sub=n1-n2;
mul=n1*n2;
div=n1/n2;
System.out.println("The Sum is="+sum);
System.out.println("The Difference is="+sub);
System.out.println("The Product is="+mul);
System.out.println("The Quotient is="+div);
} }
No comments:
Post a Comment