1 -Create a Computation class with a default constructor (without parameters) allowing
to perform various calculations on integers numbers.
2. Create a method called Factorialf) which allows to calculate the factorial of an
integer. Test the method by instantiating the class.
3 - Create a method called Sum() allowing to calculate the sum of the first n integers I -
2 + 3 + . -n. Test this method.
4 - Create a method called testPrimd in the Calculation class to test the primality of a
given integer Testris merhod.
1 - Create a method called testPrimsd allowing to rest if two numbers are prime
between them,
.
5 - Create a table Muitomethod which creates and displays the multiplication table of a
given integer. Then create an allTables Murit) method to display all the integer
multiplication tables 1.239
6. - Create showr) function which show the out of the above function when you call the
above function.
Create object off the above class and call the above functions.
Answers
Program in C++
#include<iostream>
using namespace std;
class Computation
{
private:
int n;
public:
Computation()
{
cout<<"Enter a number : ";
cin>>n;
}
void Factorial()
{
int fact = 1;
for(int i = 1 ; i <= n ; i++)
{
fact = fact * i;
}
cout<<"Factorial = "<<fact<<endl;
}
void Sum()
{
int sum = 0;
for(int i = 1 ; i <= n ; i++)
{
sum = sum + i;
}
cout<<"Sum = "<<sum<<endl;
}
void testPrim()
{
int f = 0;
for(int i = 2 ; i <= n/2 ; i++)
{
if(n % i == 0)
{
f = 1;
break;
}
}
if(f == 0)
{
cout<<"Prime number"<<endl;
}
else
{
cout<<"Not a Prime Number"<<endl;
}
}
void testPrimes()
{
cout<<"Prime numbers are : "<<endl;
for(int i = 2 ; i <= n ; i++)
{
int f = 0;
for(int j = 2 ; j <= i/2 ; j++)
{
if(i % j == 0)
{
f = 1;
break;
}
}
if(f == 0)
{
cout<<i<<"\t";
}
}
}
void Mult()
{
cout<<"\nMultiplication table : "<<endl;
for(int i = 1 ; i <= 10 ; i++)
{
cout<<n<<" * "<<i<<" = "<<n*i<<endl;
}
}
};
int main()
{
Computation c;
c.Factorial();
c.Sum();
c.testPrim();
c.testPrimes();
c.Mult();
return 0;
}
Output:
Enter a number : 10
Factorial = 3628800
Sum = 55
Not a Prime Number
Prime numbers are :
2 3 5 7
Multiplication table :
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100