Friday, November 28, 2014

C++ Using prime number & Constructor pro



Find Prime Number in C++

#include
#include
using namespace std;
class prime
{
 int a,k,i;
 public:
 prime(int x)
 {
 a=x;
 k=1;
 {
 for(i=2;i<=a/2;i++)
 if(a%i==0)
 {
 k=0;
 break;
 }
 else
 {
 k=1;
 }
 }
 }
 void show()
 {
 if(k==1)
 cout<<"\n"<
 else
 cout<<"\n"<
 }
};
int main()
{
 int a;
 cout<<"Enter the Number:";
 cin>>a;
 prime obj(a);
 obj.show();
 getch();
 return 0;
}

Output


Enter the Number:7
7 is Prime Number.

Copy Constructor in C++

#include
#include
using namespace std;
class Example        
{
    int a,b;
    public:
    Example(int x,int y)          
   {
    a=x;
    b=y;
    cout<<"\nIm Constructor";
    }
    void Display()    
   {
    cout<<"\nValues :"<
    }
};
int main()                
{
        Example Object(10,20);
        Example Object2=Object;
        Object.Display();
        Object2.Display();
        getch();
        return 0;
}

Output


Im Constructor
Values :10      20
Values :10      20

Namespace in C++


#include
#include
using namespace std;
namespace namespacefirst
{
  int value = 5;
}
namespace namespacesecond
{
  double value = 3.1416;
 }
int main ()
{
  cout << "namespacefirst value : " <
  cout << "namespacesecond value : "<
  return 0;
}

Output

namespacefirst value : 5
namespacesecond value : 3.1416

Simple Class Program in C++

#include
#include
using namespace std;
class person
{
public:
  string name;
  int number;
};
int main()
{
    person obj;
    cout<<"Enter the Name :";
    cin>>obj.name;
    cout<<"Enter the Number :";
    cin>>obj.number;
    cout << obj.name << ": " << obj.number << endl;
    getch();
    return 0;
}

Output


Enter the Name :Byron
Enter the Number :100
Byron: 100

Find Prime Number in C++

#include
#include
using namespace std;
class prime
{
 int a,k,i;
 public:
 prime(int x)
 {
 a=x;
 }

void calculate()
 {
 k=1;
 {
 for(i=2;i<=a/2;i++)
 if(a%i==0)
 {
 k=0;
 break;
 }
 else
 {
 k=1;
 }
 }
 }
 void show()
 {
 if(k==1)
 cout<<"\n"<
 else
 cout<<"\n"<
 }
};
int main()
{
 int a;
 cout<<"Enter the Number:";
 cin>>a;
 prime obj(a);
           obj.calculate();
 obj.show();
 getch();
 return 0;
}

Output


Enter the Number:10
10 is Not Prime Numbers.

Enter the Number:7
7 is Prime Number.

No comments:

Post a Comment