Friday, 21 October 2016

Write a java program to print Fibonic series

Write a java program to print Fibonic series
   

Answer :

public class JavaFibonacciSeriesExample 
{
  public static void main(String[] args) 
  {

  //number of elements to generate in a series
     int limit = 20;
     long[] series = new long[limit];
     //create first 2 series elements
     series[0] = 0;
     series[1] = 1;
     //create the Fibonacci series and store it in an array
     for(int i=2; i < limit; i++)
     {
        series[i] = series[i-1] + series[i-2];
     }
     //print the Fibonacci series numbers
     System.out.println("Fibonacci Series upto " + limit);
     for(int i=0; i< limit; i++)
     {
        System.out.print(series[i] + " ");
     }
   }
}

   output: Fibonacci Series upto 20
   0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181


Write a java program to determine wheather the given year is LEAP year or not

Write a java program to determine wheather the given year is LEAP year or not
   

Answer :

public class DetermineLeapYearExample 
{
  public static void main(String[] args) 
  {
//year we want to check
    int year = 2004;
//if year is divisible by 4, it is a leap year
    if(year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))
    System.out.println("Year " + year + " is a leap year");
    else
    System.out.println("Year " + year + " is not a leap year");
  }
}

   output: Year 2004 is a leap year


Wednesday, 5 October 2016

Write a java program to find the Factorial of a number

Write a java program to find the Factorial of a number
   

Answer :


/*
List Even Numbers Java Example
This List Even Numbers Java Example shows how to find and list even
numbers between 1 and any given number.
*/
 public class FactorialExa1
 {

  public static void main(String[] args) 
  {
    int number = 5;
    /*
     Factorial of any number is! n.
     For example, factorial of 4 is 4*3*2*1.
    */
    
    int factorial = number;
    for(int i =(number - 1); i > 1; i--)  
    {
       factorial = factorial * i;
    }
     System.out.println("Factorial of the number is " + factorial);
  }

 }

   output: Factorial of the number is 120

Using Recursion


 class FactorialExa2
 {  
  static int factorial(int n)
  {    
    if (n == 0)    
      return 1;    
    else    
      return(n * factorial(n-1));    
  }
    
  public static void main(String args[])
  {  
    int i,fact=1;  
    int number=5;   
    fact = factorial(number);   
    System.out.println("Factorial of "+number+" is: "+fact);    
  }  
}  

   output: Factorial of 5 is 120


Write a java program to display the List of even numbers

Write a java program to display the List of even numbers
   

Answer :


/*
List Even Numbers Java Example
This List Even Numbers Java Example shows how to find and list even
numbers between 1 and any given number.
*/
 public class EvenExample
 {

  public static void main(String[] args) 
  {
        
    int limit = 30;//define limit
    System.out.println("Printing Even numbers between 1 and " +limit);

    for(int i=1; i <= limit; i++)
    {
     // if the number is divisible by 2 then it is even
        if( i % 2 == 0)
        {
            System.out.print(i + " ");
        }
    }
  }

 }

   output: Printing Even numbers between 1 and 30 
           2 4 6 8 10 12 14 16 18 20 22 24 26 28 30


Monday, 26 September 2016

Java program to print following number pattern

Java program to print following number pattern

      1
     212
    32123
   4321234
  543212345
 65432123456


Answer :

public class Pattern9 {

 public static void main(String[] args) {
  
     for(int i=1;i<7;i++)
     {
       for(int s=7-i;s>1;s--)
       {
         System.out.print(" ");
       }
   
       for(int j=i;1<=j;j--)
       {
         System.out.print(j);
       }

       for(int k=2;k<=i;k++)
       {
         System.out.print(k);
       }

       System.out.println();
     }
 }

}

Saturday, 24 September 2016

Java Program to sort Names in an alphabetical order.

Java program to sort Names in an alphabetical order
    Example:  input- 4
                    happy lucky abhay sibu
              output-abhay happy lucky sibu
 
    Example 2: input- 4 
                     arjun hapi babai lucky
               output-arjun babai hapi lucky 

Answer :

 public class SortNames
 {

  public static void main(String[] args) 
  {
        Scanner sc=new Scanner(System.in);
        System.out.println();
        
        int num=sc.nextInt();// how many names you want to sort 

        String[] s=new String[num];

        for(int i=0;i<(num);i++)
        {
           s[i]=sc.next();
        }

       int len=s.length;

       for(int i=0;i<(len);i++)
       {
          String temp;
          for(int j=0;j0)
              {
                  temp=s[j];
                  s[j]=s[j+1];
                  s[j+1]=temp;
              }
          }
       }

       for(String m:s)
       {
          System.out.print(m+" ");
       }
  }

 }

Thursday, 15 September 2016

Java program to remove duplicate character from a String

Java program to remove duplicate character from a String
    Example: input-aabcdefsdd
             output-abcdefs
    Example2:input-feedback
             output-fedback

Answer :

public class Pattern5 
{

 public static void main(String[] args) 
 {
   Scanner sc=new Scanner(System.in);
   System.out.println("Enter the String");
    String s=sc.next();
  
     String[] nt=s.split("");
     String result=nt[0];
  
     for(int i=0;i<(nt.length);i++)
     {
        if(!result.contains(nt[i]))
        {
            result=result+nt[i];
        }
   
     }
     System.out.println(result);
 }

}