Arithmetic Operators – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #JAVA#3657 siteicon   siteicon   siteicon  

Arithmetic Operators

Addition, subtraction, multiplication, division and modulus operator

Learning Objectives

  • Understanding the simple use of arithmetic operators

Source Code

TC++ #3657

Source Code

Run Output

a = 10 b = 4
a+b = 14
a-b = 6
a*b = 40
a/b = 2 (Integer Division)
a%b = 2

Code Understanding

int a=10; int b=4;
Integer variables a and b have been initialised here

int c=a+b; //addition operator
int d=a-b; //subtraction operator
int e=a*b; //multiplication operator
int f=a/b; //Division Operator
int g=a%b; //Modulus operator to get the remainder after division

System.out.println(“a = “+a+” b = “+b);
System.out.println(“a+b = “+c);
System.out.println(“a-b = “+d);
System.out.println(“a*b = “+e);
System.out.println(“a/b = “+f+” (Integer Division)”);
System.out.println(“a%b = “+g);
Operator based results are printed above.

Notes

Common Errors

  • One may often forget that in the integer form 10/4 would be 2 and not 2.5 as the fractional portion would be ignored while storing in integer form. If we use double or float form, 10/4 will have to be written as 10.0/4 and stored as double or float, then it would be 2.5.


Suggested Filename(s): ArithmeticOp.java



Share

sunmitra| Created: 1-Mar-2018 | Updated: 1-Mar-2018|






Back