Page 65 - iprime_V2.2_class8
P. 65

Example
                  Operator                 Description                   Syntax                            Output
                                                                                    (int a = 10, b = 2)
                             Returns  the  result to the  operand
                                                                                           a /= b            a = 5
                     /=      present  on the left hand side after        a /= b
                                                                                           a /= 5            a = 2
                             performing division
                             Returns the remainder to the operator
                                                                                          a %= b             a = 0
                     %=      present  on the left hand side after  a %= b
                                                                                          a %= 6             a = 4
                             performing division


                     WRITING SOME MORE PROGRAMS

                 Let us write programs using Java's various concepts.

                 Program 1: Program to calculate area and perimeter of a rectangle.
                 public class Calculate {
                        public static void main(String args[]) {

                               int length, width, area, perimeter;
                               length = 5;

                               width = 10;
                               // Area of rectangle = length * width
                                                                                                 Output
                               area = length * width;
                               // Perimeter of rectangle = 2 * (length + width)

                               perimeter = 2 * (length + width);
                               System.out.println("Area of the rectangle is: " + area);
                                 System.out.println("Perimeter  of the rectangle  is: " +
                               perimeter);

                        }
                 }
                 Program 2: Program to find the amount to be paid.

                 public class Amount {
                        public static void main(String args[]) {

                               int unitPrice, units, amount;
                               unitPrice = 20;

                               units = 100;
                                                                                                 Output
                               amount = unitPrice * units;
                               System.out.println("Total amount to be paid: " + amount);

                        }
                 }




                                                                                               Program Coding      63
   60   61   62   63   64   65   66   67   68   69   70