Simple Class Example Program For Find Prime Number In C++
Definition
The C++ programming language allows programmers to separate program-specific data types through the use of classes. Classes define types of data structures and the functions that operate on those data structures. Instances of these data types are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer.
Syntactically, classes are extensions of the C struct, which cannot contain functions or overloaded operators.
Prime Number:
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Syntax
class classname {
Access - Specifier :
Member Varibale Declaration;
Member Function Declaration;
}
Example Program
/* Example Program For Simple Class Example Program For Prime Number In C++
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
#include<iostream>
#include<conio.h>
using namespace std;
// Class Declaration
class prime {
//Member Variable Declaration
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 << "\n" << a << " is Prime Number.";
else
cout << "\n" << a << " is Not Prime Numbers.";
}
};
//Main Function
int main() {
int a;
cout << "Enter the Number:";
cin>>a;
// Object Creation For Class
prime obj(a);
// Call Member Functions
obj.calculate();
obj.show();
getch();
return 0;
}
Sample Output
Enter the Number:10
10 is Not Prime Numbers.
Enter the Number:7
7 is Prime Number.
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming