Sunday, 15 April 2012

Basic java programs


Program 1
//Find Maximum of 2 nos.
class Maxof2{
  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);
  }
}


Program 2
//Find Minimum of 2 nos. using conditional operator

class Minof2{
      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<j)?i:j;
      System.out.println(result+" is a minimum value");
  }
}


Program 3
/* Write a program that will read a float type value from the   keyboard and print the following output.
   ->Small Integer not less than the number.
   ->Given Number.
   ->Largest Integer not greater than the number.
*/
class ValueFormat{
  public static void main(String args[]){
      double i = 34.32; //given number 
      System.out.println("Small Integer not greater than the number : "+Math.ceil(i));
      System.out.println("Given Number : "+i);
      System.out.println("Largest Integer not greater than the number : "+Math.floor(i));
  }


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


Program 5
/* Write a program to display a greet message according to
   Marks obtained by student.
*/
class SwitchDemo{
      public static void main(String args[]){
          int marks = Integer.parseInt(args[0]);                //take marks as command line argument.
         switch(marks/10){
            case 10:
            case 9:
            case 8:
                     System.out.println("Excellent");
                     break;
            case 7:
                     System.out.println("Very Good");
                     break;
            case 6:
                     System.out.println("Good");
                     break;
            case 5:
                     System.out.println("Work Hard");
                     break;
            case 4:
                     System.out.println("Poor");
                     break;
            case 3:
            case 2:
            case 1:
            case 0:
                     System.out.println("Very Poor");
                     break;
            default:
                     System.out.println("Invalid value Entered");
      }
 }
}


Program 6
/*Write a program to find SUM AND PRODUCT of a given Digit. */
class Sum_Product_ofDigit{
      public static void main(String args[]){
            int num = Integer.parseInt(args[0]);         //taking value as command line argument.
            int temp = num,result=0;
            //Logic for sum of digit
            while(temp>0){
               result = result + temp;
               temp--;
            }
            System.out.println("Sum of Digit for "+num+" is : "+result);
            //Logic for product of digit
            temp = num;
            result = 1;
            while(temp > 0){
                 result = result * temp;
                 temp--;
            }
            System.out.println("Product of Digit for "+num+" is : "+result);
   }
}


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


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


Program 9
/*Write a program to find Fibonacci series of a given no.
  Example :
        Input - 8
        Output - 1 1 2 3 5 8 13 21
*/
class Fibonacci{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);                        //taking no. as command line argument.
          System.out.println("*****Fibonacci Series*****");
          int f1, f2=0, f3=1;
          for(int i=1;i<=num;i++){
             System.out.print(" "+f3+" ");
             f1 = f2;
             f2 = f3;
             f3 = f1 + f2;
          }
   }
}


Program 10
/* Write a program to find sum of all integers greater than 100 and
   less than 200 that are divisible by 7 */
class SumOfDigit{
      public static void main(String args[]){
      int result=0;
      for(int i=100;i<=200;i++){
           if(i%7==0)
              result+=i;
      }
      System.out.println("Output of Program is : "+result);
   }
}
 Related post:


6 comments:

  1. //Maximum of two numbers in C++:

    #include

    int main()
    {
    int a, b;
    std::cin>>a>>b;
    int max = a>b?a:b;
    std::cout<<max;
    return 0;
    }

    ReplyDelete
  2. Thnx Dude...other guys too can post their programs in other language or with other format..!!

    ReplyDelete
  3. Nice post, thanks for sharing this wonderful and useful information with us.
    2003 Volvo XC90 AC Compressor

    ReplyDelete
  4. Keep visiting for more updates...:P

    ReplyDelete
  5. Payroll System

    import java.io.*;
    public class employee{
    public static void main(String[]args)throws IOException{
    BufferedReader Stdin=new
    BufferedReader (new InputStreamReader(System.in));
    String x;
    int Days,Rate,Grosspay,Totaldeduction,Late,Status,Totalsalary15,Totalsalary30,Totalamount,Netpay;
    System.out.println(" Account no:");
    x=Stdin.readLine();
    System.out.println("Last Name:");
    x=Stdin.readLine();
    System.out.println(" First Name:");
    x=Stdin.readLine();
    System.out.println("Department:");
    x=Stdin.readLine();
    System.out.println("Status:");
    x=Stdin.readLine();
    Status=Integer.parseInt(x);
    if(Status>5)
    System.out.println("Regular");
    else
    System.out.println("Contractual");
    System.out.println("Days:);
    x=Stdin.readLine();
    Rate=Integer.parseInt(x);
    Grosspay=(Days*Rate);
    System.out.println("Grosspay:"+"Php "+Grosspay);
    x=Stdin.readLine();
    System.out.println("Deduction:");
    x=Stdin.readLine();
    System.out.println("SSS =35O");
    System.out.println("TAX =10%");
    System.out.println("PAG-IBIG =100");
    System.out.println("PHILHEALTH =100/monthly");
    x=Stdin.readLine();
    Totaldeduction=((Grosspay/100*10)+350+100);
    System.out.println("Total Deduction:"+"Php "+Totaldeduction);
    x=Stdin.readLine();
    Netpay=(Grosspay-Totaldeduction);
    System.out.println("Netpay:"+"Php "+Netpay);
    x=Stdin.readLine();
    System.out.println("Another Deduction:");
    x=Stdin.readLine();
    System.out.println("Canteen = 100");
    System.out.println("Load = 350");
    System.out.println("Late:");
    x=Stdin.readLine();
    Late=Integer.parseInt(x);
    Totalamount=(100+350+(Late*100));
    System.out.println("Total Amount:"+"Php "+Totalamount);
    x=Stdin.readLine();
    Totalsalary15=(Netpay-Totalamount);
    Totalsalary15=(Netpay-Totalamount)*2-100;
    System.out.println("Total Salary na talaga toh brad !by15:"+"Php"+Totalsalary15);
    System.out.println("Total Salary na talaga toh brad !by30:"+"Php"+Totalsalary30);
    }
    }

    ReplyDelete
    Replies
    1. Do this payroll system have a database??

      Delete