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

}

Java program to print following number pattern

Java program to print following number pattern
1                     1 
1 2                 2 1 
1 2 3             3 2 1 
1 2 3 4         4 3 2 1 
1 2 3 4 5     5 4 3 2 1 
1 2 3 4 5 6 6 5 4 3 2 1 


Answer :

public class Pattern5 
{

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

         for(int k=6-i;k>0;k--)
         {
            System.out.print("       ");
         }

         for(int l=i;l>0;l--)
         {
            System.out.print(l+" ");
         }

         System.out.println();
     }
  
 }

}

Java program to print following number pattern

Java program to print following number pattern
 6 5 4 3 2 1 2 3 4 5 6 
   6 5 4 3 2 3 4 5 6 
     6 5 4 3 4 5 6 
       6 5 4 5 6 
         6 5 6 
           6 


Answer :

public class Pattern6
{

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

         for(int j=6;j>=i;j--)
         {
            System.out.print(j+" ");
         }

         int t=i+1;

         while(t<(7))
         {
            System.out.print(t+" ");
            t++;
         }

         System.out.println();
     }

 }

}

Thursday, 8 September 2016

Java program to print following number pattern

Java program to print following number pattern
                       1  
                    1  2  1  
                 1  2  3  2  1  
              1  2  3  4  3  2  1  
           1  2  3  4  5  4  3  2  1  
        1  2  3  4  5  6  5  4  3  2  1  



Answer :

public class Pattern5 {

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

   int d=i-1;

   while(0<(d))
   {
     System.out.print(d+"  ");
     d--;
   }
   System.out.println();
  }

 }

}

Java program to print the below number pattern

Java program to print following pattern

 
 1 2 3 4 5 6 
 2 3 4 5 6 
 3 4 5 6 
 4 5 6 
 5 6 
 6 


Answer :

public class Pattern123 
{

 public static void main(String[] args)
 {
  
       for(int i=1;i<7;i++)
       {       
         for(int j=i;j<7;j++)
         {

           System.out.print(j+" ");
   
         }//end of inner for loop
       System.out.println();
       }//end of outer for loop

 }
}

Wednesday, 7 September 2016

Java program to print below pattern

Java program to print following pattern

 6 5 4 3 2 1 
 6 5 4 3 2 
 6 5 4 3 
 6 5 4 
 6 5 
 6 

Answer :

public class Pattern3 {

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

Java program to print following pattern

Java program to print following pattern

 1 
 1  * 
 1  * 3 
 1  * 3  * 
 1  * 3  * 5 

Answer :

package pattren;

public class Pattern2 {

 public static void main(String[] args) {
  
 for(int i=1;i<6;i++){
  for(int j=1;j<=i;j++){
   if(j%2==0)
              System.out.print(" * ");// Every even column will print * 
   else
     System.out.print(j+" ");
   }

   System.out.println();
  }

 }

}


Java program to print this pattern

Java program to print following pattern
Example:
 
 1
 **
 123
 ****
 12345 

Answer :

public class Pattern123 {

 public static void main(String[] args) {
  
  //i for row and j for column
          for(int i=1;i<6;i++){       
  for(int j=1;j<=i;j++){
   if(i%2==0){            //Every even row will print * 
   System.out.print("*");
   }
   else{
   System.out.print(j);
   }
   
 }//end of inner for loop
  System.out.println();
 }//end of outer for loop

}
}

Tuesday, 23 August 2016

Java Program to swap first and last word of a Sentence

Java Program to swap first and last word of a Sentence

Answer :

import java.util.Scanner;

public class StringWordSwap {

 public static void main(String[] args) {
 
  
  Scanner scan=new Scanner(System.in);
  System.out.println("Enter the String :");
  String s=scan.nextLine();
  String[] ori=s.split(" ");
//split method return type is array type. Example-if input is "give your best performance"
                          //after split method ori[]={"give","your","best","performance"};
  String temp=ori[0];                    
  
  ori[0]=ori[ori.length-1]; //"performance" will store at ori[0]
  
   ori[ori.length-1]=temp;//"give" will store at last index    
  
  for(int i=0;i<(ori.length);i++){
   System.out.print(ori[i]+" ");
  }

 }
}
Example:
 input:give your best performance
 ouput:performance your best give

Sunday, 21 August 2016

Java Program to print triangle Alphabetically

Java Program to print triangle Alphabetically
Example:

A
B C
D E F
G H I J

Answer :

public class TriangleAlphabet {

 public static void main(String[] args) 
 {
  
  char c=65;
 
  int rowNum=4;
  for(int i=1;i<=rowNum;i++)
  {
   for(int j=0;j<(i);j++)
   {
    System.out.print(c+" ");
    c++;
   }
   System.out.println();
  }

A java program to find missing element in a sorted int array.

A java program to find missing element in a sorted int array
Example-input  array={1,2,4,5,6,7,9}
        output:3,8

Answer :


public class MissingElementInArray {

 public static void main(String[] args) 
 { 
  int[] arr={1,2,4,5,6,7,9};
  for(int i=0;i<(arr.length-1);i++)
  {
   int temp=arr[i];
   
     if((temp+1)!=arr[i+1])
     {
       System.out.println(temp+1);
     }
   
  }

 }

}


Sunday, 14 August 2016

Write a java program to print following pattern

Write a java program to print following pattern
0
1 0
0 1 0
1 0 1 0

Answer :

class Output2{
public static void main(String args[]){
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
 System.out.print(((i+j)%2)+" ");
 }
 System.out.print("\n");
    }
  }
}

Write a java program to print the following pattern

Write a java program to print the following pattern
1
2 3
4 5 6
7 8 9 10

Answer :

public class Triangle {

 public static void main(String[] args) {
  
  int count=1;
  for(int i=1;i<=4;i++){
   for(int j=1;j<=i;j++){
    System.out.print(count+" ");
    count++;
   }
   System.out.println();
  }

 }

}

Saturday, 30 July 2016

Write a java program to implement add() and remove() without using collection(your class should behave like a collection class)


public class Main {

 public static void main(String[] args) {
  
  
  Stack s= new Stack();
  
  s.add(10);
  
  s.add(11);
  s.add(25);
  s.remove();
  s.remove();
  
  s.show();

 }

}
public class Stack {
 
 
 public int[] data;
 public int top;
 public int size;
 public Stack() {
   top=-1;
   size=1;
  
  data=new int[size];
  
 }
 
 public void add(int value){
  
  if(top>=size-1)
   resize();
  
  data[++top]=value;
 }
 
 private void resize(){
  
  int[] temp=data;
  size=size+1;
  data= new int[size];
  
  for(int i=0; i<=top; i++){
   
   data[i]=temp[i];
  }
  
 }
 public void remove(){
  int[] temp=data;
  for(int i=0;i<(data.length-1);i++){
   temp[i]=data[i];
  }
  data=new int[data.length-1];
  for(int j=0;j<(data.length);j++){
   data[j]=temp[j];
  }
 }
 public void show(){
  for(int i=0;i<(data.length);i++){
  System.out.println(data[i]);
  }
 }

}