Vous êtes sur la page 1sur 4

#include <iostream.

h>
int isprime(int);

Prime
int isprime(int x)
{
int d= 1;
int c=1;
while(x%d==0)
{
d++;
c++;
}
if(c<=2) return 0;
else return 1;
}
int main()
{
int a;
cout<<"Enter nnumber: ";
cin>>a;
int A= isprime(a);
if(A==0) cout<<"Its A prime"<<endl;

if(A==1) cout<<"Its not prime"<<endl;


}

1. A program that returns the volume v and the surface area s of a sphere with given radius r

Code:

#include <iostream.h>
#include <math.h>
double computeVolume (double);
double computeS_area (double);
float pie = 3.1416;
double computeVolume (double v)
{
double s = 4.0/3 * pie * pow(v,3);
return s;
}
double computeS_area (double v)
{
double s_a = 4.0 * pie * pow(v,2);
return s_a;
}
int main()
{
int r;
cout<<"Enter radius: ";
cin>>r;
cout<<"Volume is: "<<computeVolume(r)<<endl;
cout<<"Surface is: "<<computeS_area(r)<<endl;
return 0;
}
Output:

1
Enter radius: 4
Volume is: 268.083
Surface is: 201.062
Press any key to continue

2. A program to find a root for a polynomial of a given maximum degree 4

Code:
#include<iostream.h>
double f(double , double , double , double , double , double );
double f(double x, double c0, double c1, double c2, double c3, double c4)
{
double A= (((c4 * x + c3)*x + c2)*x + c1)*x + c0;
return A;
}
int main()
{
float p, a, b, c, d, e;
cout<<"Enter the value of x: ";
cin>>p;
cout<<"Enter the value of c0: ";
cin>>a;
cout<<"Enter the value of c1: ";
cin>>b;
cout<<"Enter the value of c2: ";
cin>>c;
cout<<"Enter the value of c3: ";
cin>>d;
cout<<"Enter the value of c4: ";
cin>>e;
cout<<"\n\nThe function is:\n"<<"f(x)="<<e<<" x^4 + "<<d<<"x^3 +"<<c<<"x^2 + "<<b<<" x + "<<a<<"\n\n";
cout<<"The value at x="<<p<<" is: "<<f(p,a,b,c,d,e)<<endl;
return 0;
}
Output:

Enter the value of x: 3


Enter the value of c0: 2
Enter the value of c1: 5
Enter the value of c2: 7
Enter the value of c3: -5
Enter the value of c4: 2
The function is:
f(x)=2 x^4 + -5x^3 +7x^2 + 5 x + 2

The value at x=3 is: 107


Press any key to continue

3. A power() function that returns x raised to the power n

Code:
#include<iostream.h>
long power( float , float);
long power( float x, float p)

2
{
long R = 1;
for( int i = 0; i<p; i++)
R = x*R;
return R;
}
int main()
{
float a, b;
cout<<"Enter a number: ";
cin>>a;
cout<<"Enter power: ";
cin>>b;
cout<<"\nThe result is: "<<power(a,b)<<"\n\n";
return 0;
}
Output:

Enter a number: 2
Enter power: 3

The result is: 8

Press any key to continue

2 3 n
4. program to find the following series: e x  1  x  x  x  ...  x
1! 2! 3! n!

Code:

#include<iostream.h>
long fact(int);
long power( float , float );
long power( float x, float p)
{
long R = 1;
for( int i = 0; i<p; i++)
R = x*R;
return R;
}
long fact(int n)
{
long f = 1;
if(n<=1) return f;
else
{
f = n * fact(n-1);
return f;
}
}
long main()
{
abc:
int x;
cout<<"Enter the value of x: ";
3
cin>>x;

double sum =0.0;


while(sum<=x)
{
sum += power(x, sum)/ fact(sum);
sum++;
}
cout<<"\nThe sum is: "<<sum<<"\n\n";
goto abc;
return 0;
}

Output:
Enter the value of x: 1

The sum is: 2

Press any key to continue

Vous aimerez peut-être aussi