Skip to content
Related Articles

Related Articles

Program to find the indefinite Integration of the given Polynomial

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 27 Sep, 2022
View Discussion
Improve Article
Save Article

Given a polynomial string str, the task is to integrate the given string and print the string after integrating it. Note: The input format is such that there is a whitespace between a term and the ‘+’ symbol. Examples:

Input: str = “4X3 + 3X1 + 2X2Output: X4 + (3/2)X2 + (2/3)X3 + C Input: str = “5X3 + 7X1 + 2X2 + 1X0Output: (5/4)X4 + (7/2)X2 + (2/3)X3 + Xq + C

Approach: The idea is to observe that when the given equation consists of multiple polynomials p(x) = p1(x) + p2(x)         , the integration of the given polynomial P(x) = P1(x) + P2(x)         . Also it is known that the indefinite integral of p(x) = AX^N         is P(x) = \frac{A*X^{N + 1}}{N + 1 }+ C         . Therefore, we split the given string and integrate every term in it. Below is the implementation of the above approach: 

CPP




// C++ program to find the indefinite
// integral of the given polynomial
   
#include "bits/stdc++.h"
#define MOD (1e9 + 7);
using ll = int64_t;
using ull = uint64_t;
#define ll long long
using namespace std;
   
// Function to perform the integral
// of each term
string inteTerm(string pTerm)
{
    // Get the coefficient
    string coeffStr = "", S = "";
    int i;
   
    // Loop to iterate and get the
    // Coefficient
    for (i = 0; pTerm[i] != 'x'; i++)
        coeffStr.push_back(pTerm[i]);
    long long coeff
        = atol(coeffStr.c_str());
   
    string powStr = "";
   
    // Loop to find the power
    // of the term
    for (i = i + 2; i != pTerm.size(); i++)
        powStr.push_back(pTerm[i]);
   
    long long power
        = atol(powStr.c_str());
    string a, b;
    ostringstream str1, str2;
   
    // For ax^n, we find a*x^(n+1)/(n+1)
    str1 << coeff;
    a = str1.str();
    power++;
    str2 << power;
    b = str2.str();
    S += "(" + a + "/" + b + ")X^" + b;
   
    return S;
}
   
// Function to find the indefinite
// integral of the given polynomial
string integrationVal(string& poly)
{
   
    // We use istringstream to get the
    // input in tokens
    istringstream is(poly);
   
    string pTerm, S = "";
   
    // Loop to iterate through
    // every term
    while (is >> pTerm) {
   
        // If the token = '+' then
        // continue with the string
        if (pTerm == "+") {
            S += " + ";
            continue;
        }
   
        if (pTerm == "-") {
            S += " - ";
            continue;
        }
   
        // Otherwise find
        // the integration of
        // that particular term
        else
            S += inteTerm(pTerm);
    }
    return S;
}
   
// Driver code
int main()
{
    string str
        = "5x^3 + 7x^1 + 2x^2 + 1x^0";
    cout << integrationVal(str)
         << " + C ";
    return 0;
}


Java




// Java program to find the indefinite
// integral of the given polynomial
import java.util.*;
 
class GFG {
 
  // Function to perform the integral
  // of each term
  static String inteTerm(String pTerm)
  {
    // Get the coefficient
    String coeffStr = "", S = "";
    int i;
 
    // Loop to iterate and get the
    // Coefficient
    for (i = 0; pTerm.charAt(i) != 'x'; i++)
      coeffStr += (pTerm.charAt(i));
    long coeff = Long.valueOf(coeffStr);
 
    String powStr = "";
 
    // Loop to find the power
    // of the term
    for (i = i + 2; i != pTerm.length(); i++)
      powStr += (pTerm.charAt(i));
 
    long power = Long.valueOf(powStr);
    String a, b;
 
    // For ax^n, we find a*x^(n+1)/(n+1)
    a = String.valueOf(coeff);
    power++;
    b = String.valueOf(power);
    S += "(" + String.valueOf(a) + "/"
      + String.valueOf(b) + ")X^"
      + String.valueOf(b);
 
    return S;
  }
 
  // Function to find the indefinite
  // integral of the given polynomial
  static String integrationVal(String poly)
  {
 
    // We use iStringstream to get the
    // input in tokens
    String[] is1 = poly.split(" ");
 
    String S = "";
 
    // Loop to iterate through
    // every term
    for (String pTerm : is1) {
 
      // If the token = '+' then
      // continue with the String
      if (pTerm.equals("+")) {
        S += " + ";
        continue;
      }
 
      if (pTerm.equals("-")) {
        S += " - ";
        continue;
      }
 
      // Otherwise find
      // the integration of
      // that particular term
      else
        S += inteTerm(pTerm);
    }
    return S;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    String str = "5x^3 + 7x^1 + 2x^2 + 1x^0";
    System.out.println(integrationVal(str) + " + C ");
  }
}
 
// This code is contributed by phasing17


Python3




# Python3 program to find the indefinite
# integral of the given polynomial
MOD = 1000000007
 
# Function to perform the integral
# of each term
def inteTerm( pTerm):
 
    # Get the coefficient
    coeffStr = ""
    S = "";
   
    # Loop to iterate and get the
    # Coefficient
    i = 0
    while pTerm[i] != 'x':
        coeffStr += (pTerm[i]);
        i += 1
    coeff = int(coeffStr)
   
    powStr = "";
   
    # Loop to find the power
    # of the term
    for j in range(i + 2, len(pTerm)):
        powStr += (pTerm[j]);
   
    power = int(powStr)
    a = ""
    b = "";
   
    # For ax^n, we find a*x^(n+1)/(n+1)
    str1 = coeff;
    a = str1
    power += 1
    str2 = power;
    b = str2
    S += "(" + str(a) + "/" + str(b) + ")X^" + str(b);
   
    return S;
 
   
# Function to find the indefinite
# integral of the given polynomial
def integrationVal(poly):
 
    # We use istringstream to get the
    # input in tokens
    is1 = poly.split();
   
    S = "";
   
    # Loop to iterate through
    # every term
    for pTerm in is1:
     
        # If the token = '+' then
        # continue with the string
        if (pTerm == "+") :
            S += " + ";
            continue;
         
   
        if (pTerm == "-"):
            S += " - ";
            continue;
         
   
        # Otherwise find
        # the integration of
        # that particular term
        else:
            S += inteTerm(pTerm);
     
    return S;
 
   
# Driver code
str1 = "5x^3 + 7x^1 + 2x^2 + 1x^0";
print(integrationVal(str1) + " + C ");
 
 
# This code is contributed by phasing17


C#




// C# program to find the indefinite
// integral of the given polynomial
 
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Function to perform the integral
    // of each term
    static string inteTerm(string pTerm)
    {
        // Get the coefficient
        string coeffStr = "", S = "";
        int i;
 
        // Loop to iterate and get the
        // Coefficient
        for (i = 0; pTerm[i] != 'x'; i++)
            coeffStr += (pTerm[i]);
        long coeff = Convert.ToInt64(coeffStr);
 
        string powStr = "";
 
        // Loop to find the power
        // of the term
        for (i = i + 2; i != pTerm.Length; i++)
            powStr += (pTerm[i]);
 
        long power = Convert.ToInt64(powStr);
        string a, b;
 
        // For ax^n, we find a*x^(n+1)/(n+1)
        a = Convert.ToString(coeff);
        power++;
        b = Convert.ToString(power);
        S += "(" + Convert.ToString(a) + "/"
             + Convert.ToString(b) + ")X^"
             + Convert.ToString(b);
 
        return S;
    }
 
    // Function to find the indefinite
    // integral of the given polynomial
    static string integrationVal(string poly)
    {
 
        // We use istringstream to get the
        // input in tokens
        string[] is1 = poly.Split(" ");
 
        string S = "";
 
        // Loop to iterate through
        // every term
        foreach(string pTerm in is1)
        {
 
            // If the token = '+' then
            // continue with the string
            if (pTerm == "+") {
                S += " + ";
                continue;
            }
 
            if (pTerm == "-") {
                S += " - ";
                continue;
            }
 
            // Otherwise find
            // the integration of
            // that particular term
            else
                S += inteTerm(pTerm);
        }
        return S;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        string str = "5x^3 + 7x^1 + 2x^2 + 1x^0";
        Console.WriteLine(integrationVal(str) + " + C ");
    }
}
 
// This code is contributed by phasing17


Javascript




// JavaScript program to find the indefinite
// integral of the given polynomial
   
let MOD = (1e9 + 7);
 
   
// Function to perform the integral
// of each term
function inteTerm( pTerm)
{
    // Get the coefficient
    let coeffStr = "", S = "";
    let i;
   
    // Loop to iterate and get the
    // Coefficient
    for (i = 0; pTerm[i] != 'x'; i++)
        coeffStr += (pTerm[i]);
    let coeff
        = parseInt(coeffStr)
   
    let powStr = "";
   
    // Loop to find the power
    // of the term
    for (i = i + 2; i != pTerm.length; i++)
        powStr += (pTerm[i]);
   
    let power
        = parseInt(powStr)
    let a = "", b = "";
    let str1, str2;
   
    // For ax^n, we find a*x^(n+1)/(n+1)
    str1 = coeff;
    a = str1
    power++;
    str2 = power;
    b = str2
    S += "(" + a + "/" + b + ")X^" + b;
   
    return S;
}
   
// Function to find the indefinite
// integral of the given polynomial
function integrationVal(poly)
{
   
    // We use istringstream to get the
    // input in tokens
    let is = poly.split(" ");
   
    let pTerm, S = "";
   
    // Loop to iterate through
    // every term
    for (pTerm of is)
    {
        // If the token = '+' then
        // continue with the string
        if (pTerm == "+") {
            S += " + ";
            continue;
        }
   
        if (pTerm == "-") {
            S += " - ";
            continue;
        }
   
        // Otherwise find
        // the integration of
        // that particular term
        else
            S += inteTerm(pTerm);
    }
    return S;
}
   
// Driver code
let str = "5x^3 + 7x^1 + 2x^2 + 1x^0";
console.log(integrationVal(str) + " + C ");
 
 
// This code is contributed by phasing17


Output:

(5/4)X^4 + (7/2)X^2 + (2/3)X^3 + (1/1)X^1 + C

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!