C++ Programs‎ > ‎

Simple Program for Constructor Using C++ Programming

C++ Simple Example Programs For Beginners

This Example Programs Calculate Prime Number Using Constructor


STEP 1:  Start the program.
STEP 2:  Declare the class as Prime with data members,
                  Member functions.
STEP 3:  Consider the argument constructor Prime() with integer
STEP 4:  To cal the function calculate() and do the following steps.
STEP 5:  For i=2 to a/2 do
STEP 6:  Check if a%i==0 then set k=0 and break.
STEP 7:  Else set k value as 1.
STEP 8:  Increment the value i as 1.
STEP 9:  Check whether the k value is 1 or 0.
STEP 10:If it is 1 then display the value is a prime number.
STEP 11:Else display the value is not prime.
STEP 12:Stop the program.


class prime
                int a,k,i;
              prime(int x)
              void calculate()
void show()
                  cout<< “\n\tA is prime Number. ";
                  cout<<"\n\tA is Not prime.";
void main()
    int a;
    cout<<"\n\tEnter the Number:";
    prime obj(a);

Sample Output:

Enter the number: 7
Given number is Prime Number