Program to find 3 Consecutive Terms in A.P. based on their Sum and Product

 This article deals with the program to determine 3 consecutive terms in Arithmetic Progression solely based on their sum and product. This is a grade 10  Math problem, whose solution is converted to C++ code. 


The following header files are used -

  • iostream - For Input or output stream.
  • cmath - For math operations.


The following user-defined functions are created -

  • solve - This function computes and displays the result. It takes the sum and product as input and does not return any value.

C++ Code - 

// sum and product in A.P. by Vishruth Codes
#include<iostream>
#include<cmath>
using namespace std;

//Function to determine the 3 consecutive nos
void solve(int s, int p)
{
    int a,d, num1,num2,num3;
    a=s/3;
    d=sqrt((a*a*a-p)/a);
    num1=a;
    num2=a-d;
    num3=a+d;
    cout<<"\nThe 3 numbers are: "<<num1<<", "<<num2<<", "<<num3<<".";
}

int main()
{
    int sum, pro;
    cout<<"\nEnter the sum of 3 CONSECUTIVE terms: ";
    cin>>sum;
    cout<<"\nEnter the product of those 3 terms: ";
    cin>>pro;
    solve(sum,pro);
    return 0;
}


Subject - 

In the above example, I have implemented a C++ program to compute 3 consecutive integers in an A.P. based on their sum and product.


Working - 

The code is self-explanatory.


Output - 

Math Problem to C++ code
3 Consecutive terms in A.P.



Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.

Post a Comment

0 Comments