arithmetical progression using C++

In the program given below, we have used a arithmetical progression formula of mathematics.

Formula is:

  An = a+(n+1)d

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

Before starting 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

Here we used common difference to check entered values are in progression form or not.

here is the program,

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>  // system("pause");

 void main()
  {
     clrscr();

     //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 = 14

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

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

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

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

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

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

Thus this program will execute successfully.

Here is also video of this output where we used different arithmetical progression.