Page 125 - CA_Blue( J )_Class9
P. 125

Program 3      Write a Java program to print the cost of 25 pens if the cost of 40 pens is Rs. 2000.

                   1      class initialization

                   2      {
                   3          public static void main()

                   4          {
                   5              double a,b,c,d;

                   6              a=25;
                   7              b=40;

                   8              c=2000;
                   9              d=c/b*a;

                  10              System.out.println("Cost of 25 pens "+d);
                  11          }

                  12      }

                 You will get the following output:











                     6.4 INPUT USING PARAMETERS
                 We can take values using parameters. There are two ways to take input: using command-line arguments or by
                 passing arguments during a method call when the program is executed.

                 6.4.1 Using Command Line Arguments
                 We use command-line arguments to input values as strings in the main() method, as it is the first method invoked
                 when the program starts. These values are passed as a String array and can be used in the program. If a value
                 needs to be used as an integer or a real number, it must be converted using the appropriate methods, such as
                 Integer.parseInt() for integers or Double.parseDouble() for real numbers.

                                 Write a Java program to take the radius of a circle using Command Line Arguments and print
                  Program 4
                                 the area and circumference.
                   1      class input_using_cla
                   2      {

                   3          public static void main(String args[])
                   4          {

                   5              double r,a,c;
                   6              r=Double.valueOf(args[0]);

                   7              a=3.142*r*r;


                                                                                                   Input in Java   123
   120   121   122   123   124   125   126   127   128   129   130