Program for Constructor Using C++ Programming

-

Affiliate Disclosure: Every purchase made through our affiliate links earns us a pro-rated commission without any additional cost to you. Here are more details about our affiliate disclosure.

Program to Calculate the Prime Number using Constructor in C++

Algorithm:

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 Argument.
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.

Program:
/* Program to Calculate the Prime Number using Constructor in C++
Pardeep Patel @ studywarehouse.com */

#include<iostream.h>

#include<conio.h>
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\tA is prime Number. ";
else
cout<<"\n\tA is Not prime.";
}
};

void main()
{
clrscr();
int a;
cout<<"\n\tEnter the Number:";
cin>>a;
prime obj(a);
obj.calculate();
obj.show();
getch();
}
Output

Enter the number: 7
Given number is Prime Number

Related Articles

Like our Article/ Blog? Can buy a Buttermilk for our team.. Click here

Pardeep Patelhttps://pardeeppatel.com/
Hi!, I am Pardeep Patel, an Indian passport holder, Traveler, Blogger, Story Writer. I completed my M-Tech (Computer Science) in 2016. I love to travel, eat different foods from various cuisines, experience different cultures, make new friends and meet other.

Share this article

-- Advertisement --

LEAVE A REPLY

Please enter your comment!
Please enter your name here

-- Advertisement --