arithmetical progression program in c++

arithmetical progression in C++, here we used the formula from mathematics class 10th this is Example of using the formula in C++. we can use any type formula in C++.

The Formula is:

an= a+(n+1)d

In the program, we have asked the user to enter arithmetical progression and using the formula we find out the value of an, its step by step solution is also given.

Before starting the program, what is arithmetical progression?

As we know that, in increasing order in arithmetical progression, the difference of each term are always equal.

such as,

arithmetical progression program in cpp

Here we used for loop and array to perform arithmetical progression in C++ and the common difference used to check entered values are available in progression form or not.

Here is the program,

#include<iostream>
#include<dos.h>  // system("pause");
using namespace std;

 int main()
  {
     

     //using float data-type because terms maybe in decimal form
     float pro[5];
     float an,a,n,d;
     float d1,d2,d3;

     cout<<"Enter arithmatic Progression Terms(Max=5th): ";

      for(int term = 0; term<5; term++) 
       { 
          cin>>pro[term]; //storing terms in an array
       }

      //calculating term's common difference
      d1 = pro[1]-pro[0];
      d2 = pro[2]-pro[1];
      d3 = pro[3]-pro[2];

      //entered terms is true or false using common difference
       if(d1!=d2 || d2!=d3 || d3!=d1)
	{
	  cout<<"something is wrong...";
	  getch();
	  exit(0);
	}

      cout<<"Your Progression Terms is:-\n";
      cout<<endl<<pro[0]<<", "<<pro[1]<<", "<<pro[2]<<", "<<pro[3]<<"....."<<pro[4]<<endl;

      cout<<endl;
      system("PAUSE");

      //solution will be appear in scond secreen
      clrscr();
      cout<<"Solution:-\n"<<"\nusing formula: an = a + (n-1)d, Where-\n";

      //assining value
       an = pro[4];
       a = pro[0];
       d = d1;

      cout<<"\nan = "<<pro[4];
      cout<<"\na = "<<pro[0];
      cout<<"\nd = "<<pro[1]<<" - "<<pro[0]<<" = "<<pro[1]-pro[0];
      cout<<"\nn = ?"<<endl;

     //calculating answer
      cout<<"\t"<<an<<" = "<<a<<" + (n + 1)"<<d<<endl;
      cout<<"\t"<<an<<" = "<<a<<" + "<<d<<"n + "<<1*d<<endl;
      cout<<"\t"<<an<<" = "<<a+(1*d)<<" + "<<d<<"n"<<endl;
      cout<<"    "<<an<<" - "<<(a+(1*d))<<" = "<<d<<"n"<<endl;
      cout<<"\t"<<an-(a+(1*d))<<" = "<<d<<"n"<<endl;
      cout<<"      "<<an-(a+(1*d))<<"/"<<d<<" = n"<<endl;
      cout<<"\t"<<(an-(a+(1*d)))/d<<" = "<<"n"<<endl;

	//final answer
	cout<<"\nAnswer:- \n";
	cout<<"\tn = "<<(an-(a+(1*d)))/d;

   getch();
  }

OUTPUT

Enter arithmatic Progression Terms(Max=5th): 2 4 6 8 32
Your Progression Terms is:-
                                                         
2, 4, 6, 8.....32
                                                    
Press any key to continue.
Solution:-
                                                                                
using formula: An = a + (n-1)d, Where-
                                    
an = 32
a = 2
d = 4 - 2 = 2
n = ?
        32 = 2 + (n + 1)2
        32 = 2 + 2n + 2
        32 = 4 + 2n
    32 - 4 = 2n
        28 = 2n
      28/2 = n
        14 = n
                                  
Answer:-
        n = 1

Explanation

firstly, in the program, we declared all variable as float data-type Because arithmetical progress can also be in decimal form.

arithmetical Progression is stored in an array-variable,

float pro[5];

Suppose the arithmetical progression entered by the user is as follows-

2 4 6 8........32

here is assigned to different variables according to the formula so that the program can be easily understood. such as,

d1 = pro[1]-pro[0]; 
d2 = pro[2]-pro[1]; 
d3 = pro[3]-pro[2];

जहाँ variable d1,d2,d3 सर्वान्तर है।

where variables d1, d2, d3 are common difference.

after this, common difference indicate that  series is a arithmetical progression or not

if(d1!=d2 || d2!=d3 || d3!=d1)

if series is a arithmetical progression then formula implemented and values will be assign, such as-

an = pro[4];
a = pro[0];
d = d1;

that is,

 an  = 32
  a  = 2
  d  = 2

After this, the common-difference is used to check whether the user entered arithmetical progression is valid or not using if-statement. such as,

after all this, in the next formula will be implemented and we get the answer.

Thus this program will execute successfully.

Related exercise

Like it?

Leave a Reply

Your email address will not be published. Required fields are marked *