Friday, August 19, 2011

Java Program Source Code to Add Two Polynomials


The Following Java Program can be used to add two polynomials of any degree.
 package poly;  
 /**  
  *  
  * @author www.c-madeeasy.blogspot.com  
  */import java.io.*;  
 class node  
 {  
   int coef;  
   int pow;  
   node next;  
   node(int c,int p)  
   {  
     coef=c;  
     pow=p;  
     next=null;  
   }  
   void display()  
   {  
     System.out.println("coef="+coef+" pow="+pow);  
   }  
 }  
 class list  
 {  
   node first=null;  
   void insert(int c,int p)  
   {  
     node n1=new node(c,p);  
     node curr;  
     curr=first;  
     if(curr==null)  
       first=n1;  
     else  
     {  
       n1.next=first;  
       first=n1;  
     }  
   }  
   void display()  
   {  
     node curr;  
     curr=first;  
     while(curr!=null)  
     {  
       curr.display();  
       curr=curr.next;  
     }  
   }  
  }  
 public class Poly {  
   /**  
    * @param args the command line arguments  
    */  
   public static void main(String[] args) {DataInputStream get=new DataInputStream(System.in);  
     int c,i,a,n;  
     node curr1,curr2;  
     list l1=new list();  
     list l2=new list();  
     list l3=new list();  
     try  
     {  
      System.out.println("Enter the polynomial degree:");  
      n=Integer.parseInt(get.readLine());  
      System.out.println("first polynomial:");  
      for(i=0;i<=n;i++)  
      {  
        System.out.println("Enter coeff. of term of power "+i);  
        c=Integer.parseInt(get.readLine());  
        l1.insert(c,i);  
      }  
      System.out.println("second polynomial:");  
      for(i=0;i<=n;i++)  
      {  
        System.out.println("Enter coeff. of term of power "+i);  
        c=Integer.parseInt(get.readLine());  
        l2.insert(c,i);  
      }  
      curr1=l1.first;  
      curr2=l2.first;  
      while((curr1!=null)&&(curr2!=null))  
      {  
        a=curr1.coef+curr2.coef;        
        int p=curr1.pow;  
        l3.insert(a,p);  
        curr1=curr1.next;  
        curr2=curr2.next;  
      }  
      System.out.println("Polynomial after addition");  
      l3.display();  
     }  
     catch(Exception e)  
     {  
       System.out.println(e.getMessage());  
     }  
     // TODO code application logic here  
   }  
 }  

6 comments:

  • Techman says:
    September 6, 2011 at 10:24 PM

    If you have any queries.Please Comment on the post or ask it in the support page.

  • Anonymous says:
    October 21, 2011 at 9:51 AM

    If the polynomials have M and N terms, respectively, can you please clarify the time complexity of your program then.

  • Techman says:
    October 21, 2011 at 11:12 PM

    To easily calculate the time complexity just count the total number of basic operations which take a constant time.eg:curr=first is a basic operation.

  • This comment has been removed by the author.
    Hassnain Jamil says:
    April 19, 2015 at 10:52 AM

    This comment has been removed by the author.

  • Hassnain Jamil says:
    April 19, 2015 at 1:23 PM

    Assalam o Alaikum. This code only works for polynomials with the same degree. To make it more efficient please add the blow code after while(curr1 != null && curr2 != null) loop

    while(curr1 != null && curr2 != null){
    int add = curr1.coef + curr2.coef;
    list3.insert(add, curr1.exp);
    curr1 = curr1.next;
    curr2 = curr2.next;
    }

    if(curr1 == null){
    while(curr2 != null){
    list3.insert(curr2.coef, curr2.exp);
    curr2 = curr2.next;
    }
    }

    if(curr2 == null){
    while(curr1 != null){
    list3.insert(curr1.coef, curr1.exp);
    curr1 = curr1.next;
    }
    }

Post a Comment

Subscribe

The Source Codes Published in this Blog can be used freely for Educational purposes but should not be reproduced on any other Blog or Website without the consent of the author.