Answer to Question #8705 in C++ for ANKIT PANDYA

Question #8705
program to exemplify constructor
1
Expert's answer
2012-04-26T07:28:33-0400
This example calculates prime number using constructor.

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.

C++ PROGRAM:

#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS