Page 158 - ComputerScience_Class_11
P. 158

Here,
              •  a and b are operands.
              •  + and = are operators.
              •  5 is a constant.

              7.3.1 Types of Operators
              There are many types of operators. They are as follows:
              •  Arithmetic operators                    •  Logical operators
              •  Assignment operator                     •  Shorthand operators

              •  Bitwise operators                       •  Relational operators
              •  Conditional assignment operators

              Arithmetic Operators
              An arithmetic operator is used for writing codes that perform arithmetical calculations.

              The table given below shows the binary arithmetic operators which deal with two operands:
                       Operator       Meaning                        Description                     Example
                          +       Addition         It is used to find the sum of the values of the two operands. a + b
                           -      Subtraction      It is used to find the difference of the values of the two  a - b
                                                   operands.
                          *       Multiplication   It is used to find the product of the two operands.  a * b
                          /       Division         It is used to find the quotient of the two operands.  a / b
                          %       Modulus          It is used to find the remainder of the two operands.  a % b
                                                   It works on both integer and floating point data types.

                              Write a program that prompts the user to enter two numbers and performs the arithmetic
                Program 2
                              operations.
                1       import java.util.*;
                2       class arithmetic
                3       {
                4           public static void main(String[] args)
                5           {
                6               Scanner sc = new Scanner(System.in);
                7               int a, b;
                8               System.out.print("Enter 1st number: ");
                9               a = sc.nextInt();
                10              System.out.print("Enter 2nd number: ");
                11              b = sc.nextInt();
                12              System.out.println("Sum: "+(a+b));
                13              System.out.println("Product: "+(a*b));
                14              System.out.println("Difference: "+(a-b));
                15              System.out.println("Quotient: "+((double)a/(double)b));
                16              System.out.println("Remainder: "+(a%b));
                17          }
                18      }




                  156  Touchpad Computer Science (Ver. 3.0)-XI
   153   154   155   156   157   158   159   160   161   162   163