Find the integral roots of a given Cubic equation | C ++ program for the above approach ; Function to find the value at x of the given equation ; Find the value equation at x ; Return the value of ans ; Function to find the integral solution of the given equation ; Initialise start and end ; Implement Binary Search ; Find mid ; Find the value of f ( x ) using current mid ; Check if current mid satisfy the equation ; Print mid and return ; Print " NA " if not found any integral solution ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int check ( int A , int B , int C , int D , long long int x ) { long long int ans ; ans = ( A * x * x * x + B * x * x + C * x + D ) ; return ans ; } void findSolution ( int A , int B , int C , int D , int E ) { int start = 0 , end = 100000 ; long long int mid , ans ; while ( start <= end ) { mid = start + ( end - start ) / 2 ; ans = check ( A , B , C , D , mid ) ; if ( ans == E ) { cout << mid << endl ; return ; } if ( ans < E ) start = mid + 1 ; else end = mid - 1 ; } cout << " NA " ; } int main ( ) { int A = 1 , B = 0 , C = 0 ; int D = 0 , E = 27 ; findSolution ( A , B , C , D , E ) ; }