C++ Programming

C++ Program to a Number is Prime or Not

Assume that you want to check whether the No. is prime or not. Write a Program that ask for a No. representing. When it finishes the calculation, the program asks if the user wants to do another calculation. The response may be yes or no (Y\N).

Source Code

#include<iostream>

#include<conio.h>

using namespace std;

class prime

{

int d;

public:

void getn()

{

    cout<<"Enter a number :";

    cin>>d;

}

void isprime()

{

int i,flag=0;

for(i=2;i<=(d/2);i++)

{

    if((d%i)==0)

{

flag=1;

break;

}

    }

        if(flag)

        cout<<"Composite!!";

        else

        cout<<"Prime!!";

    }

};

int main()

{

        prime a;

        char ch;

        do

        {

            a.getn();

            a.isprime();

            cout<<endl<<"Do you wish to do another calculation (Y/N)?";

            cin>>ch;

        }

        while(ch!='N') ;

}

Output

prime or not

Back to top button