C ++ Programs
Welcome to C++ Programs and example. it is actually designed to help understands and master C++ Programming language
C++ hello world example
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
return 0;
}
C++ hello world class program
#include<iostream>
using namespace std;
// Creating class
class Message
{
public:
void display() {
cout << "Hello World\n";
}
};
int main()
{
Message c; // Creating an object
c.display(); // Calling the function
return 0;
}
C++ programming code
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "Enter two integers to add\n";
cin >> a >> b;
c = a + b;
cout <<"Sum of the numbers: " << c << endl;
return 0;
}
C++ program to reverse a number
#include <iostream>
using namespace std;
class Operations
{
long c;
public:
void inputNumber()
{
cout << "Input a number\n";
cin >> c;
}
long reverseNumber()
{
long invert = 0;
while (c != 0)
{
invert = invert * 10;
invert = invert + c%10;
c = c/10;
}
return invert;
}
};
int main()
{
long result;
Operations t;
t.inputNumber();
result = t.reverseNumber();
cout << "Number obtained on reversal = " << result;
return 0;
}
C++ program to add two complex numbers
#include <iostream>
using namespace std;
class complex
{
public :
int real, img;
};
int main()
{
complex a, b, c;
cout << "Enter a and b where a + ib is the first complex number.";
cout << "\na = ";
cin >> a.real;
cout << "b = ";
cin >> a.img;
cout << "Enter c and d where c + id is the second complex number.";
cout << "\nc = ";
cin >> b.real;
cout << "d = ";
cin >> b.img;
c.real = a.real + b.real;
c.img = a.img + b.img;
if (c.img >= 0)
cout << "Sum of two complex numbers = " << c.real << " + " << c.img << "i";
else
cout << "Sum of two complex numbers = " << c.real << " " << c.img << "i";
return 0;
C++ programming code
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n, status = 1, num = 3, count, c;
cout << "Enter the number of prime numbers to print\n";
cin >> n;
if ( n >= 1 )
{
cout << "First " << n <<" prime numbers are :-" << endl;
cout << 2 << endl;
}
for ( count = 2 ; count <=n ; )
{
for ( c = 2 ; c <= (int)sqrt(num) ; c++ )
{
if ( num%c == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
cout << num << endl;
count++;
}
status = 1;
num++;
}
return 0;
C++ programming code
#include<iostream>
using namespace std;
main()
{
int n, c, first = 0, second = 1, next;
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are :- " << endl;
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
}
return 0;
}
C++ programming code
#include<iostream>
using namespace std;
main()
{
int first[20], second[20], c, n;
cout << "Enter the number of elements in the array ";
cin >> n;
cout << "Enter elements of first array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> first[c];
cout << "Enter elements of second array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> second[c];
cout << "Sum of elements of two arrays " << endl;
for ( c = 0 ; c < n ; c++ )
cout << first[c] + second[c] << endl;
return 0;
}
C++programming code
#include<iostream>
using namespace std;
main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout << "Enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "Enter the elements of first matrix\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];
cout << "Enter the elements of second matrix\n";
for ( c = 0 ; c < m ;c++ )
for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
cout << "Sum of entered matrices:-\n";
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";
cout << endl;
}
return 0;
}
C++ programming code
#include<iostream>
#include<cstdlib>
using namespace std;
main()
{
int n, max, num, c;
cout << "Enter the number of random numbers you want ";
cin >> n;
cout << "Enter the maximum value of random number ";
cin >> max;
cout << "Random numbers from 0 to " << max << " are:" << endl;
for ( c = 1 ; c <= n ; c++ )
{
num = random(max);
cout << num << endl;
}
return 0;
}
C++ programming code
#include<iostream>
using namespace std;
class programming
{
private:
int variable;
public:
void input_value()
{
cout << "In function input_value, Enter an integer\n";
cin >> variable;
}
void output_value()
{
cout << "Variable entered is ";
cout << variable << "\n";
}
};
main()
{
programming object;
object.input_value();
object.output_value();
//object.variable; Will produce an error because variable is private
return 0;
}
}
Good one
ReplyDeleteNice one
ReplyDeleteVery good
ReplyDelete