Sunday, October 16, 2011

Java


Java Program To Find Whether Number Is Prime or Not


/* 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
             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");
    }
}
Java program to find whether given number is Armstrong or not

/*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");
   }
}


Simple java programs to generate a Triangle and Invert triangle

/*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");
           } 
    }
}






/* 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--;
            }
      }
}



A simple multiplication table using java


/* 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");
      }
  }
}


Basic java program to concatenate string using for LoOP

Basic java program to revere a number

/* 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);
  }
}



/*Write a program to Reverse a given no. */

class Reverse{
      public static void main(String args[]){
        //take argument as command line
          int num = Integer.parseInt(args[0]);           
          int remainder, result=0;
          while(num>0){
              remainder = num%10;
              result = result * 10 + remainder;
              num = num/10;
         }
         System.out.println("Reverse number is : "+result);
    }
}


Simple java program for finding factorial of a number


/*Write a program to Find Factorial of Given no. */

class Factorial{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);     
         //take argument as command line
          int result = 1;
          while(num>0){
                result = result * num;
                num--;
          }
          System.out.println("Factorial of Given no. is : "+result);
   }
}


Simple java program which generates five random numbers between 1to 1oo

The below program describes a java program which generates five random numbers Between 1to 1oo using Math.random



/*Write a program to generate 5 Random nos. between 1 to 100, and it should not follow with decimal point.
*/
class RandomDemo{
      public static void main(String args[]){
          for(int i=1;i<=5;i++){
              System.out.println((int)(Math.random()*100));
          }
    }
}



A Basic java program for finding maximum of two numbers and min


class Maxoftwo{
  public static void main(String args[]){
      //taking value as command line argument.
      //Converting String format to Integer value
      int i = Integer.parseInt(args[0]);
      int j = Integer.parseInt(args[1]);
      if(i> j)
          System.out.println(i+" is greater than "+j);
      else
          System.out.println(j+" is greater than "+i);
  }
}

output:

javac Maxoftwo.java
java Maxoftwo 10 20
    20 is greater then 10

//end 

/* can also write minimum of two nos program using conditional operator */ 


//Find Minimum of 2 nos. using conditional operator

class Minoftwo{
      public static void main(String args[]){
      //taking value as command line argument.
      //Converting String format to Integer value
      int i = Integer.parseInt(args[0]);
      int j = Integer.parseInt(args[1]);
      int result = (i
      System.out.println(result+" is a minimum value");
  }
}
  




BASIC JAVA PROGRAMS FOR BIGINNERS


        Hi, now i am going to post about the java language and some basic programs on it. these programs may be useful for the graduate level core java. In parallel i will post the programs for the advanced level.Now as a basic programmer you need to implement the experiments on the programs by modification on statements while in the execution process

To do this you require to install the software called  " jdk for java se(standard edition)" availiable at .

http://www.oracle.com/technetwork/java/javase/downloads/index.html

and notepad as a text editor.

As a formality i am posting "Hello World!" as a starting program

class HelloWorldExample{
    public static void main(String[] args) {     
System.out.println("Hello World!");
    }
}

--EVERY PROGRAM IN JAVA ARE IN THE FORM OF CLASSES
  that's why we used  "class" as starting line with the unique name for its class  as "HelloworldExample"
--Next one is MAIN METHOD
   In the Java programming language, every application must contain a main method whose signature is:
public static void main(String[] args)
The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv". Here the main method is an entrance to the program, so absolutely it is required. 
public indicates that the main() method can be called by any object. 
static indicates that the main() method is a class method. 
void indicates that the main() method has no return value.
 compile it with
javac HelloWorldExample.java
run it with the command 
java HelloWorldExample 
 
The output can be   
Hello World!
That's about the basic story of a hello world program in java. If you know more explanation please post it in comments.

No comments:

Post a Comment