Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, 26 August 2012

Java Program to convert given no. of days into months and days


 Java Program to convert given no. of days into months and days.
  (Assuming each month is of 30 days)

Class DayMonth{
      public static void main(String args[]){
      int number = Integer.parseInt(args[0]);
      int days = number%30;
      int month = number/30;
      System.out.println(number+" days = "+month+" Month and "+days+" days");
   }
}

Java Program to Display Multiplication Table


Java Program to Display Multiplication Table

Class MultiTable{
      public static void main(String args[]){
      int number = Integer.parseInt(args[0]);
      System.out.println("*****MULTIPLICATION TABLE*****");
      for(int i=1;i<=number;i++){
         for(int j=1;j<=number;j++){
            System.out.print(" "+i*j+" ");
         }
         System.out.print("\n");
      }
  }
}

Saturday, 18 August 2012

Java Program to find Maximum of 2 Numbers


Java Program to find Maximum of 2 numbers:
Class MaxNo{
  public static void main(String args[]){
      //taking value as command line argument.
      //Converting String format to Integer value
      int n1 = Integer.parseInt(args[0]);
      int n2 = Integer.parseInt(args[1]);
      if(n1 > n2)
          System.out.println(n1+" is greater than "+n2);
      else
          System.out.println(n2+" is greater than "+n1);
  }
}

Java program to find SUM AND PRODUCT of a given Digit

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

Java program to find sum of all integers greater than 100 and less than 200 that are divisible by 7


Java program to find sum of all integers greater than 100 and less than 200 that are divisible by 7:
class SumOfSeries{
      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("Sum of the series is : "+result);
   }
}

Java Program to generate the Fibonacci series of a given number:


Java Program to generate the Fibonacci series of a given number:
//Input Number: 7
// Output - 1 1 2 3 5 8 13
class Fibonacci{
      public static void main(String args[]){
          int number = Integer.parseInt(args[0]);                        //taking no. as command line argument.
          System.out.println("*****Fibonacci Series*****");
          int a, b=0, c=1;
          for(int i=1;i<=num;i++){
             System.out.print(" "+c+" ");
             a = b;
             b = c;
             c = a+b;
          }
   }
}

Java program to find a factorial of a given number


Java program to find a factorial of a  given number:
class FactorialNo{
      public static void main(String args[]){
       int  number = Integer.parseInt(args[0]);                 //take argument as command line
          int result = 1;
          while(number>0){
                result = result * number;
                number--;
          }
          System.out.println("Factorial of Given number is : "+result);
   }
}

Sunday, 12 August 2012

Java Program - To check whether the Number is palindrome or not.


Public Class PalindromeNo
{
   public static void main(String args[])
   {
      int Originalno= Integer.ParseInt(args[0]);
      int temp, revNo = 0;
      int no=OriginalNo;
       while( no != 0 )
      {
          revNo = revNo * 10;
          revNo = revNo + no%10;
          no = no/10;
      }

If(OriginalNo== revNo)
 System.out.println(OriginalNo+”is a palindrome Number”);
else
System.out.println(OriginalNo+”is a palindrome Number”);
}
}

Java Program- To Reverse a Number


Public Class ReverseNo
{
   public static void main(String args[])
   {
      int no= Integer.ParseInt(args[0]);
      int rev = 0;
 
       while( no != 0 )
      {
          rev = rev * 10;
          rev = rev + no%10;
          no = no/10;
      }

      System.out.println("Reverse of the given number is "+rev);
   }
}

Saturday, 11 August 2012

Java Program to Swap two numbers without using third (temporary) variable

Java Program to swap two numbers without using third (temporary) variable

Program :

Class Swap{
public static void main(String args[]){
int n1,n2;
n1=2;
n2=3;
System.out.println(“Before swap n1=”+n1 +”\nn2=”+n2);
n1=n1+n2;
n2=n1-n2;
n1=n1-n2;
System.out.println(“After swap n1=”+n1 +”\nn2=”+n2);
}}