Saturday, January 26, 2013

Write a C++ program calculates the following equation for entered numbers (n, x). 1+1/1+1/1+(sqrt x)..................n terms

Exercise 4)
Write a C++ program and call it series2.cpp which receives two numbers from input (n, x) and calculates the following equation for entered numbers (n, x).
1+1/1+1/1+(sqrt x)..................n terms

Solution-


#include<iostream>
#include<cmath>

using namespace std;


int main()
{
//-------defining variables and initializing them-------------    
    double n,x,i,ans,ans1;
    char redo;
//--------Printing my name on screen----------------    
    cout<<"Welcome to the n term series program  written by Your Name"<<endl;
    cout<<"***************************************************************"<<endl;
    cout<<endl<<endl<<endl;
//--here do loop is used so that the program can be used more then one time
//without exiting the run screen---------------------------    
    do
    {
 //----receiving the variables from input--------------         
    
     cout<<" Please enter two numbers to apply your requested operation"<<endl;
    cout<<"enter the value of n:";
    cin>>n;
    cout<<"enter the value of x:" ;
    cin>>x; 
    cout<<endl;
 // initialising for loop   
    for(i=0;i<=n;i++)
    {
 // the desired expression has the below formulae    
 //when u enter n=0 and x=0 the ans will be 1.5 because our series starts from(1+1/ans1)               
         ans1= (1+1/(1+sqrt(x)));     
         ans=(1+1/ans1);
         }
         //------- printing the results on screen----------- 
         cout<<"the ans of this series="<<ans<<endl;
         //cout<<ans1;        
         
    
          cout<<endl;   
     //----now once again the program will ask the user if want to continue or not          
           cout<<"enter y or Y to continue:";
           cin>>redo;
           cout<<endl<<endl;

           }
           while(redo=='y'||redo=='Y');
           
   system("pause");
    return 0;
    
    }





---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------

No comments:

Post a Comment