Sunday, January 9, 2022

AP series addition using friend function

 #include <iostream>

using namespace std;
class AP
{
    int terms;

public:
    int a, b;
    void setdata(int a)
    {
        terms = a;
    }
    friend void takeAP(AP);
    void getdata()
    {
        cout << "enter the first term " << endl;
        cin >> a;
        cout << "enter the common difference " << endl;
        cin >> b;
        cout << "enter the number of terms " << endl;
        cin >> terms;
    }
    friend int withdata(AP);
    friend void printseries(AP);
   
};
void takeAP(AP o1)
{
    int term[o1.terms];
    cout << "enter terms" << endl;
    for (int i = 0; i < o1.terms; i++)
    {
        cin >> term[i];
    }
    int term1, lastterm, commondifference, sum;
    term1 = term[0];
    lastterm = term[(o1.terms) - 1];
    commondifference = term[1] - term1;
    // n/2[2a + (n – 1)d]
    sum = (o1.terms) * (term1 + lastterm);
    sum = sum / 2;
    cout << "your first term is " << term1 << endl;
    cout << "your last term is " << lastterm << endl;
    cout << "your commondifference is " << commondifference << endl;
    cout << "sum is " << sum << endl;
}

int withdata(AP o2)
{

    int sum;
    sum = ((o2.terms) * ((2 * (o2.a)) + (o2.terms - 1) * (o2.b))) / 2;

    return sum;
}
void printseries(AP o3){
    cout<<"the series is :"<<endl;
    for (int i = 0; i < o3.terms ; i++)
    {
        cout<<o3.a <<" , ";
        o3.a = o3.a + o3.b;
    }
   
}
int main()
{
    int a1, choice;
    AP x;
    cout << "choose 1 if you have series choose 2 if you have data" << endl;
    cin >> choice;
    if (choice == 1)
    {
        cout << "enter the number of terms in AP you want to add " << endl;
        cin >> a1;
        x.setdata(a1);
        takeAP(x);
       
    }
    else if (choice == 2)
    {
        x.getdata();
        cout << "the sum is " << withdata(x) << endl;
        printseries(x);
   
    }
    else
    {
        cout << "wrong!" << endl;
    }

    return 0;
}

No comments:

Software scope

 In software engineering, the software scope refers to the boundaries and limitations of a software project. It defines what the software wi...

Popular Posts