Thursday, November 28, 2013

Program using if - else if - else construct




Q- 1.
Input number of calls, calculate and print the bill.

          For first 50 calls               0
          For next 150 calls             1 Re/Call
          For next 150 calls             .75 p/Call
          For next 150 calls             .60 p/Call
          Over 500 calls                  .5 p/Call
          Add : Rs. 180 as rent and 12.05% as service tax.



//Telephone Bill
import java.io.*;
public class Elec_Bill
    {
        public static void main(String args[])throws IOException
            {
                int call;
                double bill=0;
                BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("Enter no of calls : ");
                call=Integer.parseInt(Br.readLine());
                if(call<=50)
                    {
                        bill=180;
                    }
                else if(call<=200)
                    {
                        bill=180+(call-50)*1 ;
                    }
                else if(call<=350)
                    {
                        bill=180+(100*1)+(call-150)*0.75 ;
                    }
                else if(call<=500)
                    {
                        bill=180+(100*1)+(150*0.75)+(call-350)*0.6 ;
                    }
                else
                    {
                         bill=180+(100*1)+(150*0.75)+(150*0.6)+(call-500)*0.5;
                    }      
                bill=bill+(bill*12.05/100.0);
                System.out.println("\n\tThe Telephone bill is Rs."+bill);
        }
    }



Q-2.
        a) Largest of two number
        b) Largest of three Number

//largest of three using logical operator
import java.io.*;
public class Large
    {
        public static void main(String args[])throws IOException
            {
                int a,b,c;
                BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("Enter First No. : ");
                a=Integer.parseInt(Br.readLine());
                System.out.print("Enter Second No. : ");
                b=Integer.parseInt(Br.readLine());
                System.out.print("Enter Third No. : ");
                c=Integer.parseInt(Br.readLine());               
                Large2(a,b);
                Large3(a,b,c);

        }
        public static void Large2(int a, int b)
            {
                if(a>b)
                    {
                        System.out.println("\n\t"+a+" is the largest");
                    }
                else
                    {
                        System.out.println("\n\t"+b+" is the largest");
                    }
            }
        public static void Large3(int a, int b,int c)
            {
                if(a>b && a>c)
                    {
                        System.out.println("\n\t"+a+" is the largest");
                    }
                else if(b>c)
                    {
                        System.out.println("\n\t"+b+" is the largest");
                    }
                else
                    {
                        System.out.println("\n\t"+c+" is the largest");;
                    }           
            }       

    }


Program using ternary operator

Q-1.
          a) Largest of two number


    public class Ternary_Operator
        {
            public static void main(String args[])
                {
                    int a=10,b=20;
                    System.out.println("The largest between a and b ("+a+","+b+") = "+(a>b?a:b));
                }
            }



          b) Largest of three Number


    import java.io.*;   
    public class Ternary_Operator_1
        {
            public static void main(String args[])throws IOException
                {
                    int a=10,b=20,c=5;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("Enter First No. : ");
                    a=Integer.parseInt(Br.readLine());
                    System.out.print("Enter Second No. : ");
                    b=Integer.parseInt(Br.readLine());
                    System.out.print("Enter Third No. : ");
                    c=Integer.parseInt(Br.readLine());               
                    System.out.println("The largest between a and b ("+a+","+b+","+c+") = "+(a>b?(a>c? a : c):(b>c?b:c)));
                }
            }

No comments: