Monday, 31 August 2015

Number pattern to print outer layer of diamond

public class NumberPatternOuterDiamond
{
/*
*    1
   2   2
 3       3
4           4
 3       3
   2   2
     1
*/
public static void main(String[] args) {
int i,j,k;
 for(i=1;i<=4;i++)
 {
   for(j=4;j>=(i-1)*2-1;j--)
     System.out.print(" ");
   System.out.print(i);
   for(j=2;j<=(i-1)*4;j++)
    System.out.print(" ");
   if(i>1)
    System.out.print(i);
   System.out.println();
 }
 for(i=3;i>=1;i--)
 {
   for(j=4;j>=(i-1)*2-1;j--)
    System.out.print(" ");
   System.out.print(i);
   for(j=2;j<=(i-1)*4;j++)
    System.out.print(" ");
   if(i>1)
    System.out.print(i);
   System.out.println();
 }
}
}
Number pattern to print like X

public class NumberPatternInX
{
/*
* 1     1
2   2
 3 3
  4  
 3 3
2   2
1     1
*/
public static void main(String[] args) {
int i,j,k=1;
   int m[][] = new int[7][7];
   for(i=1;i<=7;i++)
   {
       for(j=1;j<=7;j++)
           if(j==i || 8-i==j)
               m[i-1][j-1]=k;
           if(i<4) k++;
           else --k;

   }
   for(i=0;i<7;i++)
   {
       for(j=0;j<7;j++)
       {
           if(m[i][j]==0)
               System.out.print(" ");
           else
               System.out.print(m[i][j]);
       }
       System.out.println();
   }
}
}

Number pattern

public class NumberPattern5
{
/*
* 12345
    1234
    123
    12
    1
*/

public static void main(String[] args) {
int i, j;
   for(i=5;i>=1;i--)
   {
       for(j=1;j<=i;j++)
       {
           System.out.print(j);
       }
       System.out.println();
   }
}
}
Number pattern with 1 in sqare

public class NumberPattern1insquare
{
/*
* 11111
1      1
1      1
1      1
11111
*/
public static void main(String[] args) {
int i,j;
   for(i=1;i<=5;i++)
   {
       for(j=1;j<=5;j++)
       {
           if(j==5 || j==1 || i==1 || i==5)
               System.out.print("1");
           else
              System.out.print(" ");
       }
       System.out.println();
   }
}
}
Number pattern 1,10....

public class NumberPattern10
{
/*
*     1
10
101
1010
10101
*/
public static void main(String[] args) {
int i,j,k;
 for(i=1;i<=5;i++)
 {
   for(j=1;j<=i;j++)
   {
     System.out.print(j%2);
   }
   System.out.println();
 }
}
}
Diamond pattern using JAVA

public class Diamond_pattern
{
/*
*     1
*   121   
* 12321
*   121      
*    1
*/

public static void main(String[] args)
{
int noofCols=1;
int noofspaces=2;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=noofspaces;j++)
{
System.out.print(" ");
}
if(i<3)
{
noofspaces=noofspaces-1;
}
else
{
noofspaces=noofspaces+1;
}
int k=0;
for(int j=1;j<=noofCols;j++)
{
if(j<=i)
{
k=k+1;
}else
{
k=k-1;
}
System.out.print(k);

}
System.out.println();
if(i<3)
{
noofCols=noofCols+2;
}
else
{
noofCols=noofCols-2;
}
}

}

}

Friday, 31 July 2015

The java code to create a Singly Linked List

1)First create a class which has two data members,

package singly_linked_list;

public class NODE
{
int data;
NODE next;//self pointer
}

2)Then utilize the above class by using reference variables as pointers and to insert the data create a instance of it. I have created a class i.e "AscendingLinkedList" this class will insert the data in a ascending order.

package singly_linked_list;

import java.util.Scanner;

public class AscendingLinkedList
{

NODE head = null;
public static void main(String[] args)
{
AscendingLinkedList n1 = new AscendingLinkedList();
boolean conti =true;
while(conti)
{
System.out.println("singly linked list pgm started");
System.out.println("enter your choice");
System.out.println("1.insert");
System.out.println("2.display");

Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();

switch (choice)
{
case 1: n1.insert();
break;
case 2: n1.display();
break;
default: System.exit(0);
break;
}
System.out.println("do you want to continue, if yes press 1 else 0");
int ch = sc.nextInt();
if(ch==1){conti = true;}else{conti  = false;}
}//end of while
System.out.println("sinlgy linked list pgm ended");
}//end of main

public void insert()
{
System.out.println("enter the number to insert");

Scanner sc1 = new Scanner(System.in);

int number  = sc1.nextInt();
NODE cur =null;
NODE new_node=null;

if(head==null)
{
System.out.println("The list is empty inserting the first element");
head =new NODE();
head.data= number;
head.next=null;
System.out.println("insertion successfull!!!!!!!!!");
}

else if(number<=head.data)
{
insert_front(number);
}
else
{
new_node = new NODE();

System.out.println("inserting at the rear");
cur = head;
while(cur.next!=null)
{
cur=cur.next;
}//end of while
cur.next = new_node;
new_node.data = number;
new_node.next = null;
System.out.println("insertion successfull!!!!!!!!!");
}
}//end of insert

public void insert_front(int number)
{
System.out.println("inserting at the front");
NODE cur =null;
NODE new_node=new NODE();

cur = head;
new_node.data = number;
new_node.next=cur;
head=new_node;
System.out.println("insertion successfull!!!!!!!!!");

}//end of inser_front

public void display()
{
NODE cur =null;
if(head.next==null)
{
System.out.print(head.data+"-->");
System.out.print("null");
}
else{
System.out.println("the content of the list are:");
cur=head;
while(cur.next!=null)
{
System.out.print(cur.data+"-->");
cur=cur.next;
}//end of while
System.out.print(cur.data+"-->");
System.out.println("null");
}//end of if_else
}//end of display
}//end of class Nodes

3) Create an another class by name "DescendingLinkedList" that should insert in a descending order.