Home » » Java Programs 1-9

Java Programs 1-9

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




Program 1

/* Write a program to Concatenate  string using for Loop

   Example:
          Input - 5
          Output - 1 2 3 4 5 */
class Join{
      public static void main(String args[]){
      int num = Integer.parseInt(args[0]);
      String result = " ";
      for(int i=1;i<=num;i++){
           result = result + i + " ";
      }
      System.out.println(result);
  }
}

Program 2
/* Program to Display Multiplication Table */
class MultiplicationTable{
      public static void main(String args[]){
      int num = Integer.parseInt(args[0]);
      System.out.println("*****MULTIPLICATION TABLE*****");
      for(int i=1;i<=num;i++){
         for(int j=1;j<=num;j++){
            System.out.print(" "+i*j+" ");
         }
         System.out.print("\n");
      }
  }
}

Program 3
/* Write a program to Swap the values */
class Swap{
      public static void main(String args[]){
      int num1 = Integer.parseInt(args[0]);
      int num2 = Integer.parseInt(args[1]);
      System.out.println("\n***Before Swapping***");
      System.out.println("Number 1 : "+num1);
      System.out.println("Number 2 : "+num2);
      //Swap logic
      num1 = num1 + num2;
      num2 = num1 - num2;
      num1 = num1 - num2;
      System.out.println("\n***After Swapping***");
      System.out.println("Number 1 : "+num1);
      System.out.println("Number 2 : "+num2);
      }
}

Program 4
/* Write a program to convert given no. of days into months and days.
  (Assume that each month is of 30 days)
  Example :
           Input - 69
           Output - 69 days = 2 Month and 9 days */
class DayMonthDemo{
      public static void main(String args[]){
      int num = Integer.parseInt(args[0]);
      int days = num%30;
      int month = num/30;
      System.out.println(num+" days = "+month+" Month and "+days+" days");
   }
}

Program 5
/*Write a program to generate a Triangle.
  eg:
  1
  2 2
  3 3 3
  4 4 4 4 and so on as per user given number */
class Triangle{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);
          for(int i=1;i<=num;i++){
             for(int j=1;j<=i;j++){
                System.out.print(" "+i+" ");
             }
             System.out.print("\n");
           }
    }
}

Program 6
/* Write a program to Display Invert Triangle.
   Example:
          Input - 5
          Output :
          5 5 5 5 5
          4 4 4 4
          3 3 3
          2 2
          1
*/
class InvertTriangle{
      public static void main(String args[]){
           int num = Integer.parseInt(args[0]);
           while(num > 0){
              for(int j=1;j<=num;j++){
                  System.out.print(" "+num+" ");
                }
                System.out.print("\n");
                num--;
            }
      }
}

Program 7
/*Write a program to find whether given no. is Armstrong or not.
  Example :
           Input - 153
           Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no. */
class Armstrong{
      public static void main(String args[]){
      int num = Integer.parseInt(args[0]);
      int n = num; //use to check at last time
      int check=0,remainder;
      while(num > 0){
           remainder = num % 10;
           check = check + (int)Math.pow(remainder,3);
           num = num / 10;
      }
      if(check == n)
            System.out.println(n+" is an Armstrong Number");
      else
            System.out.println(n+" is not a Armstrong Number");
   }
}

Program 8
/* Write a program to Find whether number is Prime or Not. */
class PrimeNo{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);
         int flag=0;
         for(int i=2;i<num;i++){
             if(num%i==0)
              {
                 System.out.println(num+" is not a Prime Number");
                 flag = 1;
                 break;
              }
         }
         if(flag==0)
             System.out.println(num+" is a Prime Number");
    }
}

Program 9
/* Write a program to find whether no. is palindrome or not.
   Example :
           Input - 12521 is a palindrome no.
           Input - 12345 is not a palindrome no. */
class Palindrome{
      public static void main(String args[]){
          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;
           }
          if(reverse == n)
              System.out.println(n+" is a Palindrome Number");
          else
              System.out.println(n+" is not a Palindrome Number");
     }
}

0 comments:

Post a Comment

Followers