Introduction:-
Hello friends, In order to learn Java programming you must be very clear in Java fundamentals. You must have clear idea say how to use different kinds of operators in Java source code. Today in order to make you familiar with Operators used in Java, I will be providing you tutorial on Arithmetic Operator. Read more If you want some depth understanding on what are Arithmetic operator in Java ?. Here in this hub I will be telling you basic use of Arithmetic operator. I will be presenting before you the source code and than try to explain you code step-by-step. Our program code in this hub will require Java's basic knowledge such as : How to write your first Java program ?. and how to create, compile and run a java program ?.
Arithmetic Operator Explanation :-
The Java programming language uses arithmetic operators very extensively. Arithmetic operators are used where we have to perform basic mathematical computations. The mathematical computations such as addition, subtraction, division, multiplication etc . The operators used are: + for addition, - for subtraction, * for multiplication, / for division and % for modulus operator.
Program to demonstrate working of Arithmetic Operators :-
Output and Explanation of working of above sample code :-
When we run above program by command say : java Arithmetic 10 6 We get the following output on the console window:
Line 6 to 10:- All the lines from 6 to 10 do the Arithmetic Operations. Line 6 performs the addition of a and b by the use of + operator.Line 7 performs the multiplication of a and b by the use of * operator. Line 8 performs the division of a by b by the use of / operator. Line 9 performs the mod of a by b by the use of % operator. Line 10 performs the subtraction of b from a by the use of - operator.
Line 12 to 17:- Lines from 12 to 17 just prints the values calculated by applying above Arithmetic Operators. It uses System.out.println() method to perform all printing of values to console window.
Hello friends, In order to learn Java programming you must be very clear in Java fundamentals. You must have clear idea say how to use different kinds of operators in Java source code. Today in order to make you familiar with Operators used in Java, I will be providing you tutorial on Arithmetic Operator. Read more If you want some depth understanding on what are Arithmetic operator in Java ?. Here in this hub I will be telling you basic use of Arithmetic operator. I will be presenting before you the source code and than try to explain you code step-by-step. Our program code in this hub will require Java's basic knowledge such as : How to write your first Java program ?. and how to create, compile and run a java program ?.
Arithmetic Operator Explanation :-
The Java programming language uses arithmetic operators very extensively. Arithmetic operators are used where we have to perform basic mathematical computations. The mathematical computations such as addition, subtraction, division, multiplication etc . The operators used are: + for addition, - for subtraction, * for multiplication, / for division and % for modulus operator.
Program to demonstrate working of Arithmetic Operators :-
public class Arithmetic { public static void main(String[ ] args) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int sum = a + b; int prod = a * b; int quot = a / b; int rem = a % b; int sub = a - b; System.out.println(a + " + " + b + " = " + sum); System.out.println(a + " * " + b + " = " + prod); System.out.println(a + " / " + b + " = " + quot); System.out.println(a + " - " + b + " = " + sub); System.out.println(a + " % " + b + " = " + rem); System.out.println(a+ " = " +quot+ " * " +b+ " + " +rem); } }
Output and Explanation of working of above sample code :-
When we run above program by command say : java Arithmetic 10 6 We get the following output on the console window:
10 + 6 = 16Here we pass two command-line arguments "10" and "6". These two strings are passed as an argument to main method and gets stored in the form of an array. Where args[0] is assigned "10" value and args[1] is assigned "6" value. In order to use numeric value in program we need to parse string tointeger value. To do parsing we use Integer class which has method called as parseInt. This is static method and has argument which is a String. This method takes the String value and parses to numeric type. This values are assigned to int variables a and b.
10 * 6 = 60
10 / 6 = 1
10 - 6 = 4
10 % 6 = 4
10 = 1 * 6 + 4
Line 6 to 10:- All the lines from 6 to 10 do the Arithmetic Operations. Line 6 performs the addition of a and b by the use of + operator.Line 7 performs the multiplication of a and b by the use of * operator. Line 8 performs the division of a by b by the use of / operator. Line 9 performs the mod of a by b by the use of % operator. Line 10 performs the subtraction of b from a by the use of - operator.
Line 12 to 17:- Lines from 12 to 17 just prints the values calculated by applying above Arithmetic Operators. It uses System.out.println() method to perform all printing of values to console window.