You are on page 1of 37

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++ program for prime numbers: print first n prime numbers.


#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++ program for Fibonacci series.


#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++ program to add two arrays.


#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++ program to add two matrices.


#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++ program to print random numbers


#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++ class program example


#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;
}

Function overloading in C++:


#include <iostream>
using namespace std;
/* Function arguments are of different data type */
long add(long, long);
float add(float, float);
int main()
{
long a, b, x;
float c, d, y;
cout << "Enter two integers\n";
cin >> a >> b;
x = add(a, b);
cout << "Sum of integers: " << x << endl;
cout << "Enter two floating point numbers\n";
cin >> c >> d;
y = add(c, d);
cout << "Sum of floats: " << y << endl;
return 0;
}
long add(long x, long y)
{
long sum;
sum = x + y;
return sum;
}
float add(float x, float y)
{

float sum;
sum = x + y;
return sum;
}

C++ programming code for function overloading


#include <iostream>
using namespace std;
/* Number of arguments are different */
void display(char []); // print the string passed as argument
void display(char [], char []);
int main()
{
char first[] = "C programming";
char second[] = "C++ programming";
display(first);
display(first, second);
return 0;
}
void display(char s[])
{
cout << s << endl;
}
void display(char s[], char t[])
{
cout << s << endl << t << endl;
}

Output of program:
C programming
C programming

C++ programming

C++ new operator example:


#include <iostream>
using namespace std;
int main()
{
int n, *pointer, c;
cout << "Enter an integer\n";
cin >> n;
pointer = new int[n];
cout << "Enter " << n << " integers\n";
for ( c = 0 ; c < n ; c++ )
cin >> pointer[c];
cout << "Elements entered by you are\n";
for ( c = 0 ; c < n ; c++ )
cout << pointer[c] << endl;
delete[] pointer;
return 0;
}

Scope resolution operator(::)


#include <iostream>
using namespace std;
char c = 'a';

// global variable

int main() {
char c = 'b'; //local variable
cout << "Local c: " << c << "\n";
cout << "Global c: " << ::c << "\n"; //using scope resolution operator
return 0;
}

Scope resolution operator in class


#include <iostream>
using namespace std;
class programming {
public:
void output(); //function declaration
};
// function definition outside the class
void programming::output() {
cout << "Function defined outside the class.\n";
}
int main() {
programming x;
x.output();

return 0;
}

Program to enter two integers and find their sum and


average
#include <iostream.h>
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int x,y,sum;
float average;
cout << "Enter 2 integers : " << endl;
cin>>x>>y;
sum=x+y;
average=sum/2;
cout << "The sum of " << x << " and " << y << " is " << sum << "." << endl;
cout << "The average of " << x << " and " << y << " is " << average << "." <<
endl;
getch();
}
This program takes in two integers x and y as a screen input from the user.
The sum and average of these two integers are calculated and outputted using the
'cout' command.
SAMPLE INPUT
86
SAMPLE OUTPUT
The sum of 8 and 6 is 14.
The average of 8 and 6 is 7.

Program to enter your age and print if you should be in


grade 10
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int age;
cout << "Enter your present age : " << endl;
cin>>age;
if(age==16)
{
cout << "Your present age is " << age << " years." << endl;
cout << "You are of the right age for joining grade 10 !" << endl;
}
else
{
cout << "Your present age is " << age << " years." << endl;
cout << "You are not of the right age for joining grade 10 !" << endl;
}
getch();
}
Input:
15
Output:
Your present age is 15 years.
You are not of the right age for joining grade 10 !

NO ARGUMENT NO RETURN VALUES


#include<iostream.h>
#include<conio.h>
void main()
{
void message();
clrscr();
cout<<endl;"Welcome to C++";
message();
cout<<endl<<"end";
getch();
}
void message()
{
cout<<endl<<"Welcome to the Function";
}
/* To perform Addition of two numbers
Without Argument and With Return values */
#include<iostream.h>
#include<conio.h>
int add(); //function prototype declaration
void main()
{
int c;
c=add();
/* Return Variable - c */
Cout<<"The sum of two numbers is <<c;
}
int add()
{
int a,b,c;
Cout<<"Enter two Numbers=";
Cin>>a>>b;
c=a+b;
return(c);
}

WITH ARGUMENT NO RETURN VALUES


#include<iostream.h>
void main()
{
int b,h;
void area(int,int);
cout<<endl<<"Enter Base and Height Values";
cin>>b>>h;
area(b,h);
cout<<endl<<"end";
}
void area(int x,int y)
{
float at;
at=0.5*x*y;
cout<<"area is :"<<at;
}
WITH ARGUMENT WITH RETURN VALUES
#include<iostream.h>
#include<conio.h>
void main()
{
int b,h;
float a;
float area(int,int);
clrscr();
cout<<endl<<"Enter Base and Height Values";
cin>>b>>h;
a=area(b,h);
cout<<endl<<"Area of the Triangle is "<<a;
getch();
}
float area(int x,int y)
{
float at;
at=0.5*x*y;
return at;
}

Write a Program to calculate the area of a circle using class & Object
#include<iostream.h>
#include<conio.h>
class raji
{
private:
int radius;
float area;
public:
void getdata()
{
cout<<"Enter the radius"<<endl;
cin>>radius;
}
void calculate()
{
area=3.14*radius*radius;
}
void showdata()
{
cout<<"The area of the circle is "<<area<<endl;
}
};
void main()
{
raji r;
r.getdata();
r.calculate();
r.showdata();
getch();
}

ARRAY AS CLASS MEMBER DATA


#include<iostream.h>
#include<conio.h>
const int MAX=100;
class stack
{
private:
int st[MAX];
int top;
public :
stack() {
top=0; }
void push(int v) {
st [++top]=v; }
int pop()
{
return st[top--];
}
};
void main()
{
stack s1;
s1.push(11);
s1.push(22);
cout<<" Ist Data :"<<s1.pop(); cout<<" IInd Data :"<<s1.pop();
s1.push(33);
s1.push(44);
s1.push(55);
s1.push(66);
cout<<" III Data :"<<s1.pop()<<endl;
cout<<" IV Data :"<<s1.pop()<<endl;
cout<<" V Data :"<<s1.pop()<<endl;
cout<<" VI Data :"<<s1.pop()<<endl;
getch();
}

ARRAY OF OBJECTS
#include<iostream.h>
#include<conio.h>
class circle {
private:
int area, radius;
public:
void getdata() {
cout<<"Enter the radius value"<<endl;
cin>>radius;
}
void calculate() {
area=3.14*radius*radius;
}
void show() {
cout<<"the area is "<<area<<endl;
}
};
void main() {
int i;
circle e[5];
for(i=0;i<5;i++)
{
e[i].getdata();
e[i].calculate();
e[i].show(); }
getch(); }
Output
Enter the radius value 1
The area is 3
Enter the radius value 2
The area is 12
Enter the radius value 3
The area is 28
Enter the radius value 4
The area is 50
Enter the radius value 4
The area is 50

FRIEND FUNCTION
#include<iostream.h>
class book
{
private :
int bno;
char bname[20];
public:
void getdata();
friend void show(book);
};
void book::getdata()
{
cin>>bno>>bname;
}
void show(book bk)
{
cout<<bk.bno<<bk.bname;
}
void main()
{
book b;
clrscr();
b.getdata();
show(b);
getch();
}

Friend Function -2
#include<iostream.h>
class second;
class first
{
private :
int no;
public:
first(int n);
friend int add(first,second);
};
class second
{
private :
int n1;
public:
second(int);
friend int add(first,second);
};
first ::first(int n)
{
no=n;
}
second ::second(int n)
{
n1=n;
}
int add(first f,second s)
{
cout<<"First class="<<f.no<<endl;
cout<<"Second class= <<s.n1;
return f.no+s.n1;
}
void main()
{
first f(10);
second s(20);
clrscr();
cout<<"\n The Result is "<<add(f,s);
}

FRIEND CLASS
#include<iostream.h>
#include<conio.h>
class second;
class first
{
private :
int no;
public:
friend class second;
first(int n)
{
no=n;
}};
class second
{
public:
void show(first f)
{
cout<<f.no;
}
};
void main()
{
first f(10);
second s;
s.show(f);
getch();
}

DEFAULT CONSTRUCTOR
#include<iostream.h>
#include<conio.h>
class exam {
private:
int sno,mark1,mark2;
public:
exam( ) {
sno = mark1 = mark2 =100;
}
void showdata()
{
cout<<"Sno"<<sno <<"Mark1 <<mark1<<"Mark2<<mark2 ;
}
exam(int a,int m1,int m2)
{
sno=a;
mark1=m1;
mark2=m2;
}};
void main()
{
exam e,e1;
clrscr();
e.showdata();
e.getdata();
e.showdata();
getch();
}

Overloaded Constructor
#include<iostream.h>
class circle
{
private:
int radius;
float area;
int b,h;
public:
circle(){
cout<<"Enter the radius "<<endl;
cin>>radius;
area=3.14 * radius * radius;
cout<<"area of the circle"<< area;
}
};
circle(int r){
radius=r;
area=3.14 * radius * radius;
cout<< "\narea of the circle is " << area; }
circle(int x,int y){
b=x;
h=y;
area=0.5*b*h;
cout<< "\n area of the Triangle is " << area;}
void main()
{
circle c;
circle d1(6);
circle c1(3,4);
}

//Example for Single inheritance


#include<iostream.h>
class pcp
{
private:
float dos,msoffice,foxpro;
public:
void pcp_getfees();
void pcp_listfees();
};
class hdca:public pcp
{
private:
float unix,c,cpp;
public:
void hdca_getfees();
void hdca_listfees();
};
void pcp::pcp_getfees()
{
cout<<"Enter the fees amount for ";
cin>>dos>>msoffice>>foxpro;
}
void pcp::pcp_listfees()
{
cout<<"Dos: "<<dos<<endl;
cout<<"Msoffice: "<<msoffice<<endl;
cout<<"Foxpro: "<<foxpro<<endl;
}
void hdca::hdca_getfees()
{
cout<<"Enter the fees amount :";
cin>>unix>>c>>cpp;
}
void hdca::hdca_listfees()
{
cout<<"Unix: "<<unix<<endl;
cout<<"C: "<<c<<endl;
cout<<"C++: "<<cpp<<endl;
}

void main()
{
clrscr();
hdca h;
cout<<endl<<"Fees detail for PCP"<<endl;
h.pcp_getfees();
cout<<endl<<"Fees detail for HDCA"<<endl;
h.hdca_getfees();
cout<<endl<<"Fees list for PCP"<<endl;
h.pcp_listfees();
cout<<endl<<"Fees list for HDCA"<<endl;
h.hdca_listfees();
getch();
}

MULTIPLE INHERITANCE
#include<iostream.h>
class base1
{
protected :
int var1;
public :
void disp_base1() {
cout<<"var1 "<<var1<<endl;
}};
class base2
{
protected :
int var2;
public :
void disp_base2()
{
cout<<"var2 is"<<var2<<endl;
}};
class deri:public base1,public base2
{
private:
int var3;
public :

deri(int a,int b,int c)


{
var1=a;
var2=b;
var3=c;
}
void disp_me()
{
cout<<"var3 is"<<var3;
} };
Void main()
{
void main()
{
deri d(10,20,30);
clrscr();
d.disp_base1();
d.disp_base2();
d.disp_me();
getch();
}

Example for Hierarchical


inheritance
#include<iostream.h>
class employee
{
private :
int empno;
char empname[20];
public :
void getdata(){
cout<<"\n Enter Employee No";
cin>>empno;
cout<<"\n Enter Employee name ";
cin>>empname;}
void putdata(){
cout<<"\n Employee No"<<empno;
cout<<"\n Employee name "<<empname;}
};

class manager :public employee


{
private :
float basic;
public :
float salary;
float da;
float hra;
float cca;
float pf;
float special_allowances;
void getdata()
{
employee::getdata();
cout<<"Enter Basic Salary \n";
cin>>basic;
}
}
void putdata()
{
da=0.30*basic;
hra=0.15*basic;
cca=0.05*basic;
pf=0.12*basic;
special_allowances=0.4*basic;
salary=basic+da+hra+cca+special_allowances-pf;
employee::putdata();
cout<<endl<<salary;
}};
class supervisor : public employee
{
private :
float basic;
public :
float salary;
float da;
float hra;
float cca;
float pf;
float special_allowances;
void getdata()

{
employee::getdata();
cout<<"Enter Basic Salary \n";
cin>>basic;
}
void putdata()
{
da=0.30*basic;
hra=0.15*basic;
cca=0.05*basic;
pf=0.12*basic;
special_allowances=0.4*basic;
salary=basic+da+hra+cca+special_allowances-pf;
employee::putdata();
cout<<endl<<salary;
}
};
class worker :public employee
{
public :
float salary;
float wages;
float incentive;
void getdata()
{
employee::getdata();
cout<<"\n Enter wages & incentives";
cin>>wages>>incentive;
}
void putdata()
{
salary=wages+incentive;
employee::putdata();
cout<<endl<<salary;
}
};
void main()
{
manager m;
supervisor s;
worker w;

clrscr();
m.getdata();
s.getdata();
w.getdata();
w.putdata();
s.putdata();
m.putdata();
getch();
}

HYBRID INHERITANCE
#include<iostream.h>
class lecturer
{
private :
char lecturer_name[20];
public :
void getdata()
{
cout<<"Enter Lecturer name";
cin>>lecturer_name;
}
void putdata()
{
cout<<"lecturer name is "<<lecturer_name;
}
};
class department : public lecturer
{
private :
char department_name[20];
public:
void getdata()
{
cout<<"Enter Department name";
cin>>department_name;
}
void putdata()
{

cout<<"Department name is "<<department_name;


}
};
class marks
{
public :
int mark1;
int mark2;
int mark3;
void getdata()
{
cout<<"Enter the marks for 3 subjects";
cin>>mark1>>mark2>>mark3;
}
void putdata()
{
cout<<"The marks for 3 subjects are "<<mark1<<"\t"<<mark2<<"\t"<<mark3;
}
};
class student:public department,public marks
{
private:
int roll_no;
char student_name[20];
public:
void getdata()
{
department::getdata();
cout<<"Enter the student name";
cin>>student_name;
cout<<"Enter the student Enrollment no";
cin>>roll_no;
marks::getdata();
}
void putdata()
{
department::putdata();
cout<<"The name & rollno is "<<student_name<<"\t"<<roll_no;
marks::putdata();
}
};

void main()
{
student s;
clrscr();
s.getdata();
s.putdata();
getch();
}

You might also like