Page 132 - CA_Blue( J )_Class9
P. 132

The compiler immediately stops the program from execution as soon as it finds such errors. So, it has to be removed
                  before execution.


                   Program 8      Write a program to print square of a number.

                     1     class syntax_error
                     2     {

                     3         public static void cal()
                     4         {

                     5             int a, sq
                     6             a = 4;

                     7             sq=a*a;
                     8             System.out.println("Square :" sq);

                     9         }
                    10     }

                  In the above example, there are two syntax errors:
                  a.  int a, sq – There is no semicolon (;) at the end of the statement

                  b.  System.out.println(“Square : “ sq); here between “ and sq there should be plus (+) to concatenate the two things
                  If these errors are removed then the program will execute.

                  6.7.2 Logical Error
                  There might be some mistake in the logic. If these types of errors are there in a program, then the program will
                  execute but it will not produce the desired output. These errors are called logical errors.


                   Program 9      Write a program to print the sum of two numbers input by the user.

                     1    import java.util.*;
                     3    public class LogicalErrorExample {

                     4        public static void main() {
                     5            Scanner sc = new Scanner(System.in);

                     6            System.out.println("Enter the first number:");
                     7            int num1 = sc.nextInt();

                     8            System.out.println("Enter the second number:");
                     9            int num2 = sc.nextInt();

                    10            int result = num1 - num2;
                    11            System.out.println("The result is: " + result);

                    12        }
                    13    }




                   130    Touchpad Computer Applications-IX
   127   128   129   130   131   132   133   134   135   136   137