Monday, September 23, 2013

Program of insertion/ deletion in an arrayList - Problem 21 P N 707 ISC Board

Define a class repeat which allows the user to add elements from one end (rear) and remove elements from the other end (front) only.
The following details of the class repeat are given below:
Class name                         :               repeat
Data members/instance variables:
St[]                                                         : an array to hold a maximum of 100 integer elements
cap                                                         : stores the capacity of the array
f                                                              : to point the index of the front
r                                                              : to point the index of the rear
Member functions:
Repeat(int m)                                    : constructor to initialize the data members cap=m, f=-1, r=-1 and to
                                                                  create the integer array
void pushvalue(int v)                     :to add integers from the rear index if possible else display the           
                                                                message (“OVERFOW”)
int popvalue()                                   : to remove and return element from the front. If array is empty  
                                                                then return -9999

void disp()                                           : display the elements present in the list

======================================================================

/**
 * class repeat inserts and deletes elements from the arraylist.
 * 
 * @author: Nitendra Verma
 * @version: Sept 23, 2013
 */
import java.util.Scanner;
class repeat
{
   protected int st[];      //array to hold elements
   protected int cap;       //stores the capacity of the array
   protected int f,r;       //to point to the first and last element 
   
   public repeat(int m)     //constructor
   {
       cap=m;
       f=-1;
       r=-1;
       st=new int[cap];
    }
    
    public boolean isEmpty()    //function- returns true if array is empty
    {
        if(f==-1)
          return true;
        return false;  
    }
    
    public void pushvalue(int v)    //function- insert a new element at the rear of the array
    {
        if(r==-1)
          {
              f=0;
              r=0;
              st[r]=v;
            }
            else if(r+1<st.length)
              st[++r]=v;
              else
              System.out.println("OVERFLOW OCCURES!!!");
    }
    
    public int popvalue()   //function- deletes and returns the last(rear) element in the array
    {
        int elem;
        boolean res;
        res=isEmpty();
        if(res==true)
          return -9999;
        else
        {
            elem=st[f];
            if(f==r)
              f=r=-1;
            else
              f++;
            return elem;
        }
    }
    
    public void disp()  //display the elements in the array
    {
        boolean res;
        res=isEmpty();
        if(res==true)
          System.out.println("No Element!!!");
         else
         {
             System.out.println("Elements are:");
             for(int i=f;i<=r;i++)
                 System.out.print(st[i]+"\t");
              System.out.println("Total number of elements:"+(r-f+1));   
            }
        
    }
}

public class test
{
  public static void main()
  {
      Scanner in=new Scanner(System.in);
      System.out.println("Eneter the capacity of array");
      int cap=in.nextInt();
      int ans;
      repeat ob=new repeat(cap);
      System.out.println("Eneter the value to be inserted");
      int item=in.nextInt();
      ob.pushvalue(item);
      
      for(int i=0;i<10;i++)
       {
       System.out.println("Do you want to insert more item? Press 1 for Yes and 2 for No.");
       ans=in.nextInt();
       if(ans==1 && ans<cap)
       {
           System.out.println("Enetr item to be pushed");
           int numnext=in.nextInt();
           ob.pushvalue(numnext);
        }
        else
           break;
    }
    System.out.println("Items in stack are:");
    ob.disp();       
    
    
       int anspop,res;
       System.out.println("Want to pop?Press 1 for Yes, 2 for No.");
       anspop=in.nextInt();
       if(anspop==1)
       {
       res=ob.popvalue();
       
       if(res==0)
           System.out.println("Underflow occurs");
       
           System.out.println("Item deleted successfully");    
        System.out.println("Items after deletion:");
        ob.disp();
    }   
}
}


  /*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in    

/*
OUTPUT:

Eneter the capacity of array
4
Eneter the value to be inserted
1
Do you want to insert more item? Press 1 for Yes and 2 for No.
1
Enetr item to be pushed
3
Do you want to insert more item? Press 1 for Yes and 2 for No.
1
Enetr item to be pushed
5
Do you want to insert more item? Press 1 for Yes and 2 for No.
1
Enetr item to be pushed
7
Do you want to insert more item? Press 1 for Yes and 2 for No.
2
Items in stack are:
Elements are:
1 3 5 7 Total number of elements:4
Want to pop?Press 1 for Yes, 2 for No.
1
Item deleted successfully
Items after deletion:
Elements are:
3 5 7 Total number of elements:3
*/

Wednesday, September 11, 2013

Program of matrix multiplictaion

/**
 * class matrixMultiple finds the mutiplication of two matirces.
 * 
 * @author: Nitendra Verma
 * @version: Sept 11, 2013
 */

import java.util.Scanner;
public class matrixMultiple
{
  public static void main()
  {
  int row,col1,col2;
  Scanner in=new Scanner(System.in);
  System.out.println("Enetr number of rows");
  row=in.nextInt();
  System.out.println("Enetr number of columns of first matrix");
  col1=in.nextInt();
  System.out.println("Enetr number of columns of second matrix");
  col2=in.nextInt();
  int[][] first=new int[row][col1];
  int[][] second=new int[row][col2];
  int[][] third=new int[row][col1];
  System.out.println("Enetr "+row*col1 +" elements of first matrix");
  for(int i=0;i<row;i++)
  {
      for(int j=0;j<col1;j++)
      {
          first[i][j]=in.nextInt();
        }
    }
    
  System.out.println("Enetr "+row*col2 +"elements of second matrix");
  for(int i=0;i<row;i++)
  {
      for(int j=0;j<col2;j++)
      {
          second[i][j]=in.nextInt();
        }
   }
   

   int sum=0;

   for(int i=0;i<row;i++)
   {
       for(int j=0;j<col2;j++)
       {
           for(int k=0;k<col1;k++)
           {
               sum+=first[i][k]*second[k][j];
            }
            third[i][j]=sum;
            sum=0;
        }
    }
    
       System.out.println("Product of above matrices");
       for(int i=0;i<row;i++)
   {
       for(int j=0;j<col1;j++)
       {
           System.out.print(third[i][j]+"\t");
        }
        System.out.println();
}
}
  }

/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in    

Monday, September 9, 2013

Single Linked List - Deletion Operations

/**
 * Deletion operations on a single linked list
 * 
 * @author: Nitendra Verma
 * @version: Sept 9,2013
 */

class listDelTest 
{
  protected static linkedList lst;
  public static void main()
  {
      int num;
      lst=new linkedList();
      lst.insert(5);
      lst.insert(3);
      lst.insert(8);
      lst.insert(9);      
      lst.insert(2);
      lst.display();
    }
}


class Node
{
 protected int data;
 protected Node link;

 public Node()
 {
     data=0;
     link=null;
    }
    
 public Node(int d,Node n)
 {
     data=d;
     link=n;
    }
    
    
 public void setdata(int d)
 {
     data=d;
    }
    
 public void setlink(Node n)
 {
     link=n;
    }
    
 public int getdata()
 {
     return data;
    }
    
 public Node getlink()
 {
    return link;
    }  
}

class linkedList
{
 protected Node start;   //start denotes the first node of the list

 public linkedList()    //default constructor
 {
     start=null;
    }
    
  public void insert(int val)
  {
      Node nptr,ptr,save=null;  
      nptr=new Node(val,null);
    boolean ins=false;
      
      //Case1: list is empty
      if(start==null)
        start=nptr;
        
      //Case2: Insertion in the beginning
      else if(val<start.getdata())     //checking whether ITEM is to be inserted in the beginning 
      {
          save=start;                  //save START's value(save is also a reference)
          start=nptr;                  //assign nptr to START
          nptr.link=save;              //makes nptr points to previous START
        }
        
        
      //Case3:INsertion in middle
      else
      {
          save=start;
          ptr=start.getlink();
          while(ptr!=null)
          {
              if(nptr.data>ptr.data)
              {
                  save=ptr;
                  ptr=ptr.link;
                }
                else            //insert in between to nodes
                {
                    save.link=nptr;
                    nptr.link=ptr;
                    break;      //jump out of loop
                }
            }
            
     //Case4:Insertion at the end      
     if(ptr==null)     
       {
           save.link=nptr;
           nptr.link=null;
           //save.setlink(nptr);
        }   
    }
}


public boolean delete(int val)
{
 boolean res=false;
 if(start.getdata()==val)   //when first node is to be deleted
 {
     start=start.getlink();
     res=true;       //node deleted
    }
    else
    {
        Node ptr,save;
        save=start;
        ptr=start.getlink();
        
        while(ptr!=null)   
        {
            if(ptr.getdata()==val)
            {
                Node next=ptr.getlink();
                save.link=next;
                res=true;     //node deleted
                break;
            }
            else
            {
                save=ptr;
                ptr=ptr.getlink();
            }
        }
    }
    return res;
}

    
    public void display()
    {
        Node ptr=start;
        System.out.print(start.getdata()+"- ->");
        ptr=start.getlink();
        
        while(ptr.getlink()!=null)
        {
            System.out.print(ptr.getdata()+"- ->");
            ptr=ptr.getlink();
        }
       System.out.print(ptr.getdata()+"!!!");    //last node info printed
       System.out.println();
    }
}

/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in    

Single Linked List- Insertion operations

/**
 * Insertion operations on a single linked list
 * 
 * @author: Nitendra Verma
 * @version: Sept 8,2013
 */

class listTest 
{
  protected static linkedList lst;
  public static void main()
  {
      int num;
      lst=new linkedList();
      lst.insert(5);
      lst.insert(3);
      lst.insert(8);
      lst.insert(9);      
      lst.insert(2);
      lst.display();
    }
}


class Node
{
 protected int data;
 protected Node link;

 public Node()
 {
     data=0;
     link=null;
    }
    
 public Node(int d,Node n)
 {
     data=d;
     link=n;
    }
    
    
 public void setdata(int d)
 {
     data=d;
    }
    
 public void setlink(Node n)
 {
     link=n;
    }
    
 public int getdata()
 {
     return data;
    }
    
 public Node getlink()
 {
    return link;
    }  
}

class linkedList
{
 protected Node start;   //start denotes the first node of the list

 public linkedList()    //default constructor
 {
     start=null;
    }
    
  public void insert(int val)
  {
      Node nptr,ptr,save=null;  
      nptr=new Node(val,null);
    boolean ins=false;
      
      //Case1: list is empty
      if(start==null)
        start=nptr;
        
      //Case2: Insertion in the beginning
      else if(val<start.getdata())     //checking whether ITEM is to be inserted in the beginning 
      {
          save=start;                  //save START's value(save is also a reference)
          start=nptr;                  //assign nptr to START
          nptr.link=save;              //makes nptr points to previous START
        }
        
        
      //Case3:INsertion in middle
      else
      {
          save=start;
          ptr=start.getlink();
          while(ptr!=null)
          {
              if(nptr.data>ptr.data)
              {
                  save=ptr;
                  ptr=ptr.link;
                }
                else            //insert in between to nodes
                {
                    save.link=nptr;
                    nptr.link=ptr;
                    break;      //jump out of loop
                }
            }
            
     //Case4:Insertion at the end      
     if(ptr==null)     
       {
           save.link=nptr;
           nptr.link=null;
           //save.setlink(nptr);
        }   
    }
}

    
    public void display()
    {
        Node ptr=start;
        System.out.print(start.getdata()+"- ->");
        ptr=start.getlink();
        
        while(ptr.getlink()!=null)
        {
            System.out.print(ptr.getdata()+"- ->");
            ptr=ptr.getlink();
        }
       System.out.print(ptr.getdata()+"!!!");    //last node info printed
       System.out.println();
    }
}

/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in    

Tuesday, June 11, 2013

Program to generate Palprime numbers(numbers which are both palindrome and prime)

PROGRAM

Palprime Numbers. Palprime numbers are the palindromic prime numbers i.e.,  the prime numbers that are palindrome also.
First palprime number is a 2-digit (2-d) number and it is 11. There is only one palprime, which is 2-digits long.
To generate a 3-digit(3-d) palprime number, there are many methods. One method is where one just needs to insert a 1-d palprime number in between 11 and then test for its primeness.
This way only two palprimes are generated(by inserting 3 and 5 between 11)
     131 and 151
(insertion of 7 and 11 does not yield a prime number)
You need to write the method GenPalPrime for the following  class that generates palprimes using the method mentioned here.
  public class PalPrime
{
  public static void GenPalPrime(int n)
  {
      /*The passed number n tells the number of digits in the palprime
        to be generated. Acceptable values for n are 2-5*/
   }
The 5-d palprimes may be generated by inserting some 3-d palprimes in 11 e.g., insertion of 131,383,797 yields these palprime numbers: 11311, 13831, 17971.
You may define additional methods, if needed.
Sample Input Output:
Enter the width of palprime numbers (ie no of digits in a palprime no):  2
11
Enter the width of palprime numbers (ie no of digits in a palprime no):  3
131

151
----------------------------------------------------------------------------------------------------------

/**
 * class PalPrime generates palprime numbers.
 * @author: Nitendra Verma 
 * @version: June 11,2013
 */
import java.util.Scanner;
public class PalPrime
{
  public static boolean isprime(int n) //method, checks whether a number is prime or not
  {
      int flag=1;
    for(int i=2;i<n;i++)  
    {
        if(n%i==0)
        {
            flag=0;
            break;
        }
    }
    if(flag==1)
      return true;
    else
      return false;
  }
  
  public static boolean ispal(int n)  //method, checks whether a number is palidrome or not
  {
      int n1,r1,ntemp,rev=0;
      ntemp=n;
      do
      {
          n1=n/10;
          r1=n%10;
          rev=rev*10+r1;
          n=n1;     
        }
        while(n1>0);
        if(rev==ntemp)
           return true;
        else
           return false;
  }

  
  public static void GenPalPrime(int n)  //method to generate numbers which are prime and palindrome both
  {
    int count=0;
    if(n==2)
    {
     System.out.println("11");
     count++;
    }
  else if(n==3)
   {
       int begnum=101;
       int lnum=0;
       boolean boolpr=false;
       boolean boolpl=false;
       for(int i=3;i<10;i++)
       {
           boolpr=isprime(i);
           boolpl=ispal(i);
           if(boolpr==true && boolpl==true)
           {
               lnum=begnum+i*10;
               if(isprime(lnum)==true && ispal(lnum)==true)
               {
               System.out.println(lnum);
               count++;
            }
            }
        }
    }
    
     else if(n==4)
   {
       int begnum=1001;
       int lnum=0;
       boolean boolpr=false;
       boolean boolpl=false;
       for(int i=10;i<100;i++)
       {
           boolpr=isprime(i);
           boolpl=ispal(i);
           if(boolpr==true && boolpl==true)
           {
               lnum=begnum+i*10;
               if(isprime(lnum)==true && ispal(lnum)==true)
               {
               System.out.println(lnum);               
               count++;
               }
            }
        }        
    }
    
    else if(n==5)
   {
       int begnum=10001;
       int lnum=0;
       boolean boolpr=false;
       boolean boolpl=false;
       for(int i=100;i<1000;i++)
       {
           boolpr=isprime(i);
           boolpl=ispal(i);
           if(boolpr==true && boolpl==true)
           {
               lnum=begnum+i*10;
               if(isprime(lnum)==true && ispal(lnum)==true)
               {
               System.out.println(lnum);
               count++;
            }
            }
        }       
    }
    else
     System.out.println("Value of n should be within range 2-5");
    if(n>=2 && n<=5 && count==0)
     System.out.println("No palprime number of this width");
}

 public static void main()
 {
     PalPrime ob=new PalPrime();  //creating object of class PalPrime
     Scanner in=new Scanner(System.in);  //creating object of class Scanner
     System.out.println("Enter the width of palprime numbers(i.e., no of digits in a palprime no):");
     int num=in.nextInt();
     GenPalPrime(num);  //method calling
 }
}

OUTPUT
Enter the width of palprime numbers(i.e., no of digits in a palprime no):
3
131
151


/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in

Program for Christian Goldback

PROGRAM
Christian Goldback has conjectured for N<1014 that every even N>2, can be represented as sum of two primes. For example, 18=5+13.

WAP that reads N which should be an even number>2 and represents it as sum of two primes.
-----------------------------------------------------------------------------------------------------

/**
 * class ChristianGoldback find two prime numbers whose sum is equal to a given number.
 * @author: Nitendra Verma 
 * @version: June 10,2013
 */
import java.util.Scanner;
public class ChristianGoldback
{
 int n,count=0;  //instance variables
    public void getnum(int nn) //method to assign parameter to number 
    {
        n=nn;
    }
    
    public void check()  //method to find prime numbers whose sum is equal to given number
    {
        int g1[]=new int[n];
        int flag=0;       
        for(int i=3;i<n;i++)  //outer loop
        {
            flag=1;
        for(int j=2;j<i;j++)  //inner loop
        {
           if(i%j==0)
           {
               flag=0;
               break;
            }           
        }
        if(flag==1)
        {
            g1[count]=i;
            count++;
        }
        
    }
        System.out.println("Number can be represented as sum of two primes:");
        for(int i=0;i<count/2;i++)
        {
            for(int j=0;j<count;j++)
            {
            if((g1[i]+g1[j])==n)
            {
                System.out.println(n +"=" +g1[i]+"+" +g1[j]);
            }
            }
        }
    }
    
    public static void main()
    {
        ChristianGoldback ob=new ChristianGoldback(); //creating object of class ChristianGoldback
        Scanner in=new Scanner(System.in);   ////creating object of class Scanner
        System.out.println("Enetr a num");
        int num=in.nextInt();   
        ob.getnum(num);     //calling method
        ob.check();         //calling method
    }
}

OUTPUT
Enetr a num
18
Number can be represented as sum of two primes:
18=5+13
18=7+11


/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in 




Program to find the sum of the series x-x2/!2+x3/!3-x4/!4+x5/!5-x6/!6

PROGRAM
WAP to find the sum of the series:
                x-x2/!2+x3/!3-x4/!4+x5/!5-x6/!6
-------------------------------------------------------------------------------------------------------------------------------
/**
 * class sumseries2 finds the sum of the series.
 * x-x2/!2+x3/!3-x4/!4+x5/!5-x6/!6
 * @author: Nitendra Verma
 * @version: June 10, 2013
 */
import java.util.Scanner;
public class sumseries2
{
  int num,limit;    //instance variables to store number and limit 
  public sumseries2()  //default constructor to inditilize variables to 0
  {
      num=0;
      limit=0;
    }    
    public void getnum(int n,int l)  //method to assign parameters to varaibles
    {
        num=n;
        limit=l;
    }
    
    public int fact(int n)  //method to find factorial value
    {
        int f=1;
        for(int i=1;i<=n;i++)
        {
            f*=i;
        }     
        return f;           //returning factorial value
    }
    
    public void findsumseries()  //method to find sum of the series 
    {
        float sum=0;
        for(int i=0;i<limit;i++)
        {
            sum+=Math.pow(-1,i)*(Math.pow(num,i+1)/fact(i+1));
        }
        System.out.println("Sum of the series:"+sum);
    }
    
    public static void main()
    {
      sumseries2 ob=new sumseries2();  //creating object of class sumseries2
      Scanner in=new Scanner(System.in); //creating object of class Scanner
      System.out.println("Enetr the number");
      int n=in.nextInt();
      System.out.println("Enetr the limit");
      int l=in.nextInt();
      ob.getnum(n,l);        //calling method
      ob.findsumseries();    //calling method
    }
}

OUTPUT
Enetr the number
2
Enetr the limit
3
Sum of the series:1.3333334



/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in