In this, we are going to see a program is based on Class which, finds that if the given number is Prime Numbers or not.
The Code given below can be used in TURBO C++ Compilers: -
#include <stdio.h>
#include<conio.h>
// Class Declaration
class prime
{
int a, k, i;
public:
prime(int x)
{
a = x;
}
// Object Creation For Class
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 << a << " is a Prime Number.";
}
else
{
cout << a << " is Not a Prime Number.";
}
}
};
void main()
{
clrscr();
int a;
cout << "Enter the Number: ";
cin >> a;
prime obj(a);
obj.calculate();
obj.show();
getch();
}
//...........Coded by Rishab Nair
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
using namespace std;
// Class Declaration
class prime
{
int a, k, i;
public:
prime(int x)
{
a = x;
}
// Object Creation For Class
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 << a << " is a Prime Number.";
}
else
{
cout << a << " is Not a Prime Number.";
}
}
};
int main()
{
int a;
cout << "Enter the Number: ";
cin >> a;
prime obj(a);
obj.calculate();
obj.show();
return 0;
}
//...........Coded by Rishab Nair
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP