Page 181 - CA_Blue( J )_Class9
P. 181

7              int n;
                   8              String remarks;

                   9              System.out.print("Enter a number : ");
                  10              n=sc.nextInt();

                  11              remarks = (n%2==0)?(n>0)?"positive even number" : "negative even number":
                  12             (n>0)? "positive odd number" : "negative odd number";

                  13              System.out.println(n +" is a "+ remarks);
                  14          }

                  15      }
                 You will get the following output:




















                  Program 4      Write a  menu-driven program to  calculate the Simple  Interest or Compound  Interest
                                 depending on the user’s choice. The principal amount, rate and time are to be accepted from
                                 the user.
                                                                      t
                                 [SI = prt/100 and CI = A - P,  A = P(1+r/100)  are the formulas of Simple Interest and Compound
                                 Interest]
                   1      import java.util.*;

                   2      class simple_compound_interest
                   3          {

                   4              public static void main()
                   5              {

                   6                  Scanner sc=new Scanner(System.in);
                   7                  double si,a,r,t,p,ci;

                   8                  int ch;
                   9                  System.out.print("Enter principal amount : ");

                  10                  p=sc.nextDouble();
                  11                  System.out.print("Enter rate : ");
                  12                  r=sc.nextDouble();





                                                                                     Conditional Constructs in Java  179
   176   177   178   179   180   181   182   183   184   185   186