Find Prime Number Example Program In C++

  #include<iostream> #include<conio.h> #include<math.h> // Math.h For sqrt function using namespace std; int main() { // Variable Declaration int n; // Get Input Value cout<<“Enter the Number :”; cin>>n; cout<<“List Of Prime Numbers Below “<<n<<endl; //for Loop Block For Find Prime Number for (int i=2; i<n; i++) for (int j=2; j*j<=i; j++) { if (i … Read more

Simple Example Program For Namespace In C++

#include <iostream> using namespace std; //Namespace namespacefirst namespace namespacefirst { int value = 5; } //Namespace namespacesecond namespace namespacesecond { double value = 3.1416; } int main () { //Namespace namespacefirst Varibale Usage cout << “namespacefirst value : ” <<namespacefirst::value << endl; //Namespace namespacesecond Varibale Usage cout << “namespacesecond value : “<<namespacesecond::value << endl; return … Read more

Simple Class Example Program For Find Prime Number In C++

#include<iostream>
#include<conio.h>
using namespace std;
// Class Declaration
class prime
{
//Member Varibale 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;
}

Read more

Simple Class Example Program In C++

  #include <iostream> #include<conio.h> using namespace std; // Class Declaration class person { //Access – Specifier public: //Varibale Declaration string name; int number; }; //Main Function int main() { // Object Creation For Class person obj; //Get Input Values For Object Varibales cout<<“Enter the Name :”; cin>>obj.name; cout<<“Enter the Number :”; cin>>obj.number; //Show the Outputcout … Read more

Simple Example Program For Copy Constructor In C++

#include<iostream> #include<conio.h> using namespace std; class Example { // Variable Declaration int a,b; public: //Constructor with Argument Example(int x,int y) { // Assign Values In Constructor a=x; b=y; cout<<”\nIm Constructor”; } void Display() { cout<<”\nValues :”<<a<<”\t”<<b; } }; int main() { Example Object(10,20); //Copy ConstructorExample Object2=Object; // Constructor invoked. Object.Display(); Object2.Display(); // Wait For Output … Read more

Simple Example Program For Constructor Overloading In C++

#include<iostream> #include<conio.h> using namespace std; class Example { // Variable Declaration int a,b; public: //Constructor wuithout Argument Example() { // Assign Values In Constructor a=50; b=100; cout<<”\nIm Constructor”; } //Constructor with Argument Example(int x,int y) { // Assign Values In Constructor a=x; b=y; cout<<”\nIm Constructor”; } void Display() {cout<<”\nValues :”<<a<<”\t”<<b; } }; int main() { … Read more

Simple Example Program For Parameterized Constructor In C++

#include<iostream> #include<conio.h> using namespace std; class Example { // Variable Declaration int a,b; public: //Constructor Example(int x,int y) { // Assign Values In Constructor a=x; b=y; cout<<“Im Constructor\n”; } void Display() { cout<<“Values :”<<a<<”\t”<<b; } }; int main() { Example Object(10,20); // Constructor invoked. Object.Display();// Wait For Output Screen getch(); return 0; }   Output … Read more

Simple Example Program For Constructor In C++

  #include<iostream> #include<conio.h> using namespace std; class Example { // Variable Declaration int a,b; public: //Constructor Example() { // Assign Values In Constructor a=10; b=20; cout<<“Im Constructor\n”; } void Display() { cout<<“Values :”<<a<<”\t”<<b; } }; int main() { Example Object; // Constructor invoked. Object.Display();// Wait For Output Screen getch(); return 0; }   Output : … Read more

Do While Loop Example Program In C++

  #include<iostream> #include<conio.h> using namespace std; int main() { // Variable Declaration int a; // Get Input Value cout<<“Enter the Number :”; cin>>a; int counter = 1; //Do while Loop Block do { cout<<“Execute Do While “<<counter<<” time”<<endl; counter++; }while (counter <= a); // Wait For Output Screen getch(); return 0; }   Output : … Read more

While Loop Example Program In C++

  #include<iostream> #include<conio.h> using namespace std; int main() { // Variable Declaration int a; // Get Input Value cout<<“Enter the Number :”; cin>>a; int counter = 1; //while Loop Block while (counter <= a) { cout<<“Execute While “<<counter<<” time”<<endl; counter++; } // Wait For Output Screen getch(); return 0; }   Output : Enter the … Read more