Home » » java Program 10-15

java Program 10-15

Written By Basith on Sunday, February 24, 2013 | 10:26 PM

Program   10
/* switch case demo
   Example :
           Input - 124
           Output - One Two Four */

class SwitchCaseDemo{
      public static void main(String args[]){
          try{
          int num = Integer.parseInt(args[0]);
          int n = num; //used at last time check
          int reverse=0,remainder;
          while(num > 0){
                remainder = num % 10;
                reverse = reverse * 10 + remainder;
                num = num / 10;
           }
          String result=""; //contains the actual output
          while(reverse > 0){
               remainder = reverse % 10;
               reverse = reverse / 10;
               switch(remainder){
                    case 0 :
                             result = result + "Zero ";
                             break;
                    case 1 :
                             result = result + "One ";
                             break;
                    case 2 :
                             result = result + "Two ";
                             break;
                    case 3 :
                             result = result + "Three ";
                             break;
                    case 4 :
                             result = result + "Four ";
                             break;
                    case 5 :
                             result = result + "Five ";
                             break;
                    case 6 :
                             result = result + "Six ";
                             break;
                    case 7 :
                             result = result + "Seven ";
                             break;
                    case 8 :
                             result = result + "Eight ";
                             break;
                    case 9 :
                             result = result + "Nine ";
                             break;
                    default:
                             result="";
                 }
            }
                System.out.println(result);
        }catch(Exception e){
             System.out.println("Invalid Number Format");
         }
     }
}

Program 11
/* Write a program to generate Harmonic Series.
   Example :
           Input - 5
           Output - 1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28 (Approximately) */
class HarmonicSeries{
      public static void main(String args[]){
      int num = Integer.parseInt(args[0]);
      double result = 0.0;
      while(num > 0){
            result = result + (double) 1 / num;
            num--;
      }
      System.out.println("Output of Harmonic Series is "+result);
  }
}

Program 12
/*Write a program to find average of consecutive N Odd no. and Even no. */
class EvenOdd_Avg{
      public static void main(String args[]){
      int n = Integer.parseInt(args[0]);
      int cntEven=0,cntOdd=0,sumEven=0,sumOdd=0;
      while(n > 0){
           if(n%2==0){
               cntEven++;
               sumEven = sumEven + n;
           }
           else{
               cntOdd++;
               sumOdd = sumOdd + n;
           }
           n--;
      }
      int evenAvg,oddAvg;
      evenAvg = sumEven/cntEven;
      oddAvg = sumOdd/cntOdd;
      System.out.println("Average of first N Even no is "+evenAvg);
      System.out.println("Average of first N Odd no is "+oddAvg);

  }
}

Program 13
/* Display Triangle as follow : BREAK DEMO.
    1
    2 3
    4 5 6
    7 8 9 10 ... N */
class Output1{
      public static void main(String args[]){
          int c=0;
          int n = Integer.parseInt(args[0]);
         loop1: for(int i=1;i<=n;i++){
         loop2: for(int j=1;j<=i;j++){
                       if(c!=n){
                            c++;
                            System.out.print(c+" ");
                       }
                       else
                           break loop1;
                    }
                    System.out.print("\n");
                 }
     }
}

Program 14
/* Display Triangle as follow
    0
    1 0
    1 0 1
    0 1 0 1 */
class Output2{
      public static void main(String args[]){
           for(int i=1;i<=4;i++){
              for(int j=1;j<=i;j++){
                            System.out.print(((i+j)%2)+" ");
                    }
                    System.out.print("\n");
                 }
     }
}      

Program 15
/* Display Triangle as follow
    1
    2 4
    3 6 9
    4 8 12 16 ... N (indicates no. of Rows) */
class Output3{
      public static void main(String args[]){
          int n = Integer.parseInt(args[0]);
                   for(int i=1;i<=n;i++){
                     for(int j=1;j<=i;j++){
                        System.out.print((i*j)+" ");
                    }
                    System.out.print("\n");
                 }
     }

}

0 comments:

Post a Comment

Followers