کلاس ریاضی - توابع: فاکتوریل ، اعداد کامل ، اعداد اول ، اعداد زوج و فرد ، تابع total
کد:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
class math
{
private:
long int number;
int x,y;
public:
long int fact (long int number)
{
if (number <= 1)
return 1;
else
return number * fact(number-1);
}
////////////////////////////////////////////////////////////////////////////////
bool even_odd (int number)
{
if(number % 2 == 0)
return true;
else
return false;
}
//////////////////////////////////////////////////////////////////////////////////
bool complete (int number)
{
int sum = 1;
for (int i=2; i < number ; i++){
if (number % i == 0)
sum += i;
}
if ( sum == number)
return true;
else
return false;
}
///////////////////////////////////////////////////////////////////////////////////
bool prim (int number)
{
for(int i=1; i<=(number/2); i++)
if(i==1 || i==number)
continue;
else if(number % i==0)
return false;
return true;
}
///////////////////////////////////////////////////////////////////////////////////
void total (int x, int y)
{
////////////prim
cout << endl<<"the prim numbers:";
for(int i=x ; i<=y ; i++)
{
if(prim(i))
cout <<i<<" , ";
}
///////////compelete
cout<<endl<<endl<<"compelete numbers :";
for(int i=x ; i<=y ; i++)
{
if(complete(i))
cout <<i<<" , ";
}
//////////even
cout <<endl<< endl<<"the even numbers:";
for(int i=x ; i<=y ; i++)
{
if(even_odd(i))
cout <<i<<" , ";
}
/////////////odd
cout <<endl<<endl<< "the odd numbers:";
for(int i=x ; i<=y ; i++)
{
if(even_odd(i)==false)
cout <<i<<" , ";
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
math m1;
long int num;
int option;
cout << "plz enter operation: fact=1,prim=2,complete=3,even_odd=4,total=5"<<endl;
cin >> option;
switch (option)
{
case 1:
cout << "plz enter a number:";
cin >> num;
cout << "factoril is:"<<m1.fact(num)<<endl;
break;
case 2:
cout << "plz enter a number:";
cin >> num;
if(m1.prim(num))
cout << "the number is prim";
else
cout << "the number isn't prim";
break;
case 3:
cout << "plz enter a number:";
cin >> num;
if(m1.complete(num))
cout << "the number is complete"<<endl;
else
cout << "the number isn't complete"<<endl;
break;
case 4:
cout << "plz enter a number:";
cin >> num;
if(m1.even_odd(num))
cout << "the number is even"<<endl;
else
cout << "the number is odd"<<endl;
break;
case 5:
int num1,num2;
cout << "enter two numbers:";
cin>>num1>>num2;
m1.total(num1,num2);
break;
default:
cout<<"I'm sorry, it's impossibe";
break;
}
_getch();
return 0;
}