Saturday, 2 June 2012

C++ Program to Calculate Polynoms

C++ Program to  Calculate Polynoms

#include <iostream.h>
#include <math.h>
#include<conio.h>
//Defining variables
int result=0; //here will be stored the result
int *arr; //here will be stored the numbers
int power; //this is the maximum power
int x; //the value for "x"

//Beginning the main function
void main()
{
 cout << "What s the maximum power in the polynom? : ";
 cin >> power;
 cout << endl;

 arr=new int[power]; //creates an array with "power" cells
 
 //Asking the user to enter the values...
 for(int n=0;n<power+1;n++)
 {
  cout <<"A"<<power-n<<" = ";
  cin >> arr[power-n];
 }

 cout << "The polynom is: ";

 //Prints the polynom on the screen
 for(int go=power; go >-1; go--)
 {
  if(go > 0)
   cout <<arr[go]<<"*x^"<<go<<" + ";
  else
   cout <<arr[go]<<"*x^"<<go;
 }
 cout <<endl;
 cout <<"Enter value for X : ";
 cin >> x;

 //Calculating the polynom
 for(int cal=0; cal < power+1; cal++)
 {
  result=result+(arr[cal]*(pow(x,cal)));
 }

 cout <<" Y(x)= "<< result<<endl; // showing the result
 getch();
}
 

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More