CS112
CS112
#include <iostream.h>
int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";
return 0;
}
Output
Enter 5 numbers: 3
4
5
4
2
Sum = 18
2. C++ if Statement
// Program to print positive number entered by the user
// If user enters negative number, it is skipped
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
Output 1
Enter an integer: 5
You entered a positive number: 5
This statement is always executed.
Output 2
Enter a number: -5
This statement is always executed.
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
if ( number >= 0)
{
cout << "You entered a positive integer: " << number << endl;
}
else
{
cout << "You entered a negative integer: " << number << endl;
}
Output
Enter an integer: -4
You entered a negative integer: -4.
This line is always printed.
2|Page
4. C++ Nested if...else
// Program to check whether an integer is positive, negative or zero
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
if ( number > 0)
{
cout << "You entered a positive integer: " << number << endl;
}
else if (number < 0)
{
cout<<"You entered a negative integer: " << number << endl;
}
else
{
cout << "You entered 0." << endl;
}
Output
Enter an integer: 0
You entered 0.
This line is always printed.
#include <iostream>
using namespace std;
int main()
{
int i, n, factorial = 1;
3|Page
cout << "Enter a positive integer: ";
cin >> n;
Output
Enter a positive integer: 5
Factorial of 5 = 120
#include <iostream>
using namespace std;
int main()
{
int number, i = 1, factorial = 1;
Output
Enter a positive integer: 4
Factorial of 4 = 24
4|Page
#include <iostream>
using namespace std;
int main()
{
float number, sum = 0.0;
do {
cout<<"Enter a number: ";
cin>>number;
sum += number;
}
while(number != 0.0);
return 0;
}
Output
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: -4
Enter a number: 2
Enter a number: 4.4
Enter a number: 2
Enter a number: 0
8. C++ break
C++ program to add all number entered by user until user enters 0.
// C++ Program to demonstrate working of break statement
#include <iostream>
using namespace std;
int main() {
float number, sum = 0.0;
if (number != 0.0)
{
sum += number;
5|Page
}
else
{
// terminates the loop if number equals 0.0
break;
}
}
cout << "Sum = " << sum;
return 0;
}
Output
Enter a number: 4
Enter a number: 3.4
Enter a number: 6.7
Enter a number: -4.5
Enter a number: 0
Sum = 9.6
9. C++ continue
include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10; ++i)
{
if ( i == 6 || i == 9)
{
continue;
}
cout << i << "\t";
}
return #0;
}
Output
1 2 3 4 5 7 8 10
6|Page
10. C++ switch Statement
// Program to built a simple calculator using switch Statement
#include <iostream>
using namespace std;
int main()
{
char o;
float num1, num2;
switch (o)
{
case '+':
cout << num1 << " + " << num2 << " = " << num1+num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1-num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1*num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1/num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! operator is not correct";
break;
}
return 0;
}
Output
Enter an operator (+, -, *, /): +
-
Enter two operands: 2.3
4.5
2.3 - 4.5 = -2.2
7|Page
11. Goto Statement
// This program calculates the average of numbers entered by user.
// If user enters negative number, it ignores the number and
// calculates the average of number entered before it.
#include <iostream>
using namespace std;
int main()
{
float num, average, sum = 0.0;
int i, n;
jump:
average = sum / (i - 1);
cout << "\nAverage = " << average;
return 0;
}
Output
Maximum number of inputs: 10
Enter n1: 2.3
Enter n2: 5.6
Enter n3: -5.6
Average = 3.95
12. C++ Program to assign data to members of a structure variable and display it.
#include <iostream.h>
8|Page
struct Person
{
char name[50];
int age;
float salary;
};
int main()
{
Person p1;
return 0;
}
Output
Enter Full name: Magdalena Dankova
Enter age: 27
Enter salary: 1024.4
Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4
struct Person {
char name[50];
int age;
float salary;
};
9|Page
Person getData(Person);
void displayData(Person);
int main()
{
Person p;
p = getData(p);
displayData(p);
return 0;
}
Person getData(Person p) {
return p;
}
void displayData(Person p)
{
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl;
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary;
}
Output
Enter Full name: Magdalena Dankova
Enter age: 27
Enter salary: 1024.4
Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4
10 | P a g e
#include <iostream.h>
struct Distance
{
int feet;
float inch;
};
int main()
{
Distance *ptr, d;
ptr = &d;
return 0;
}
Output
Enter feet: 4
Enter inch: 3.5
Displaying information.
Distance = 4 feet 3.5 inches
int main()
{
week today;
today = Wednesday;
cout << "Day " << today+1;
return 0;
}
Output
11 | P a g e
Day 4
16. Program to illustrate the working of objects and class in C++ Programming
#include <iostream.h>
class Test
{
private:
int data1;
float data2;
public:
void insertIntegerData(int d)
{
data1 = d;
cout << "Number: " << data1;
}
float insertFloatData()
{
cout << "\nEnter data: ";
cin >> data2;
return data2;
}
};
int main()
{
Test o1, o2;
float secondDataOfObject2;
o1.insertIntegerData(12);
secondDataOfObject2 = o2.insertFloatData();
Output
Number: 12
Enter data: 23.3
You entered 23.3
12 | P a g e
#include <iostream.h>
#include <conio.h>
#define PI 3.14
class Circle
{
private:
float radius;
public:
void getRadius()
{
cout << "Enter Radius ::: ";
cin >> radius;
}
void area()
{
float ar;
ar = PI * radius * radius;
cout << "\n Area of Circle : " << ar;
}
void showRadius()
{
cout << "\n Radius : " << radius;
}
};
void main()
{
clrscr();
Circle c1;
c1.getRadius();
c1.showRadius();
c1.area();
getch();
}
Output:
Radius : 10
Area of Circle : 314
class stringfun
{
char name[20];
public:
void concatString(char a[],char b[])
{
strcat(a,b);
strcpy(name,a);
}
void display()
{
cout<<"\nName : "<<name;
}
};
void main()
{
char str1[10],str2[10];
clrscr();
stringfun sf;
Output:
Enter your name:Nils
Enter sir name:Patel
Name : NilsPatel
19. Calculate the area of a rectangle and display it. (Constructor in C++)
#include <iostream.h>
14 | P a g e
class Area
{
private:
int length;
int breadth;
public:
// Constructor
Area(): length(5), breadth(2){ }
void GetLength()
{
cout << "Enter length and breadth respectively: ";
cin >> length >> breadth;
}
int main()
{
Area A1, A2;
int temp;
A1.GetLength();
temp = A1.AreaCalculation();
A1.DisplayArea(temp);
cout << endl << "Default Area when value is not taken from user" << endl;
temp = A2.AreaCalculation();
A2.DisplayArea(temp);
return 0;
}
#include <iostream.h>
#include <conio.h>
#define PI 3.14
15 | P a g e
class Circle
{
float radius;
public:
Circle();
Circle(float);
void getRadius();
float area();
void showRadius();
};
Circle :: Circle()
{
radius = 10;
}
void main()
{
clrscr();
Circle c1(10);
c1.showRadius();
float a = c1.area();
cout << "\n Area of Circle : " << a;
16 | P a g e
getch();
}
Output:
Radius : 10
Area of Circle : 314
#include <iostream>
using namespace std;
class Area
{
private:
int length;
int breadth;
public:
// Constructor with no arguments
Area(): length(5), breadth(2) { }
void GetLength()
{
cout << "Enter length and breadth respectively: ";
cin >> length >> breadth;
}
int main()
{
Area A1, A2(2, 1);
int temp;
return 0;
}
Output
Default Area when no argument is passed.
Area: 10
Area when (2,1) is passed as argument.
Area: 2
class myclass
{
private:
int t;
public:
myclass()
{
cout<<"This is A Constructor\n";
}
~myclass()
{
cout<<"This is A Destructor\n";
}
void set(int d)
{
t=d;
}
void show()
{
cout<<t<<endl;
}
};
void main()
{
clrscr();
myclass m1,m2;
m1.set(2);
18 | P a g e
m2.set(6);
m1.show();
m2.show();
cout<<"Last Statement\n";
getch();
}
Output:
This is A Constructor
This is A Constructor
2
6
Last Statement
This is A Destructor
This is A Destructor
void member::outside()
{
cout<<"This is Outside Member Function";
}
void main()
{
clrscr();
member m;
m.inside();
m.outside();
getch();
}
Output:
This is Inside Member Function
19 | P a g e
This is Outside Member Function
class stat
{
int code;
static int count;
public:
stat()
{
code=++count;
}
void showcode()
{
cout<<"\n\tObject number is :"<<code;
}
static void showcount()
{
cout<<"\n\tCount Objects :"<<count;
}
};
int stat::count;
void main()
{
clrscr();
stat obj1,obj2;
obj1.showcount();
obj1.showcode();
obj2.showcount();
obj2.showcode();
getch();
}
Output:
Count Objects :2
Object number is :1
Count Objects :2
Object number is :2
20 | P a g e
25. C++ program to show inline Function example
#include<iostream.h>
#include<conio.h>
class line
{
public:
inline float mul(float x,float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
void main()
{
line obj;
float val1,val2;
clrscr();
cout<<"Enter two values:";
cin>>val1>>val2;
cout<<"\nMultiplication value is:"<<obj.mul(val1,val2);
cout<<"\n\nCube value is
:"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
getch();
}
Output:
26. C++ program to add two complex numbers by passing objects to a function.
#include <iostream.h>
class Complex
{
private:
int real;
21 | P a g e
int imag;
public:
Complex(): real(0), imag(0) { }
void readData()
{
cout << "Enter real and imaginary number respectively:"<<endl;
cin >> real >> imag;
}
void addComplexNumbers(Complex comp1, Complex comp2)
{
// real represents the real data of object c3 because this function
is called using code c3.add(c1,c2);
real=comp1.real+comp2.real;
void displaySum()
{
cout << "Sum = " << real<< "+" << imag << "i";
}
};
int main()
{
Complex c1,c2,c3;
c1.readData();
c2.readData();
c3.addComplexNumbers(c1, c2);
c3.displaySum();
return 0;
}
Output
Enter real and imaginary number respectively:
2
4
Enter real and imaginary number respectively:
-3
4
Sum = -1+8i
22 | P a g e
27. Pass and Return Object from the Function
In this program, the sum of complex numbers (object) is returned to the main() function and displayed.
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex(): real(0), imag(0) { }
void readData()
{
cout << "Enter real and imaginary number respectively:"<<endl;
cin >> real >> imag;
}
Complex addComplexNumbers(Complex comp2)
{
Complex temp;
int main()
{
Complex c1, c2, c3;
c1.readData();
c2.readData();
c3 = c1.addComplexNumbers(c2);
c3.displayData();
return 0;
23 | P a g e
}
class Test
{
private:
int count;
public:
Test(): count(5){}
int main()
{
Test t;
// this calls "function void operator ++()" function
++t;
t.Display();
return 0;
}
Output
Count: 6
int main()
{
int *pc, c;
c = 5;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;
24 | P a g e
pc = &c; // Pointer pc holds the memory address of variable c
cout << "Address that pointer pc holds (pc): "<< pc << endl;
cout << "Content of the address pointer pc holds (*pc): " << *pc << endl
<< endl;
*pc = 2;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;
return 0;
}
Output
Address of c (&c): 0x7fff5fbff80c
Value of c (c): 5
Address that pointer pc holds (pc): 0x7fff5fbff80c
Content of the address pointer pc holds (*pc): 5
Address pointer pc holds (pc): 0x7fff5fbff80c
Content of the address pointer pc holds (*pc): 11
Address of c (&c): 0x7fff5fbff80c
Value of c (c): 2
class Circle
{
float radius;
public:
void setRadius( float radius );
float getRadius();
float area();
};
void main()
{
clrscr();
Circle c1;
c1.setRadius(10);
float a = c1.area();
cout << "\n Radius : " << c1.getRadius();
cout << "\n Area of Circle : " << a;
getch();
}
Output:
Radius : 10
Area of Circle : 314
31. C++ Program to display address of elements of an array using both array and pointers
#include <iostream.h>
int main()
{
float arr[5];
float *ptr;
// ptr = &arr[0]
ptr = arr;
26 | P a g e
cout<<"\nDisplaying address using pointers: "<< endl;
for (int i = 0; i < 5; ++i)
{
cout << "ptr + " << i << " = "<< ptr + i << endl;
}
return 0;
}
Output
Displaying address using arrays:
&arr[0] = 0x7fff5fbff880
&arr[1] = 0x7fff5fbff884
&arr[2] = 0x7fff5fbff888
&arr[3] = 0x7fff5fbff88c
&arr[4] = 0x7fff5fbff890
Displaying address using pointers:
ptr + 0 = 0x7fff5fbff880
ptr + 1 = 0x7fff5fbff884
ptr + 2 = 0x7fff5fbff888
ptr + 3 = 0x7fff5fbff88c
ptr + 4 = 0x7fff5fbff890
32. C++ Program to insert and display data entered by using pointer notation.
#include <iostream.h>
int main() {
float arr[5];
return 0;
}
Output
Enter 5 numbers: 2.5
3.5
4.5
27 | P a g e
5
2
Displaying data:
2.5
3.5
4.5
5
2
// Function prototype
void swap(int&, int&);
int main()
{
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap(a, b);
return 0;
}
Output
Before swapping
a = 1
b = 2
After swapping
a = 2
b = 1
28 | P a g e
34. Passing by reference using pointers.
#include <iostream.h>
// Function prototype
void swap(int*, int*);
int main()
{
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap(&a, &b);
Output
Before swapping
a = 1
b = 2
After swapping
a = 2
b = 1
#include <iostream.h>
class Person
{
public:
string profession;
int age;
29 | P a g e
Person(): profession("unemployed"), age(16) { }
void display()
{
cout << "My profession is: " << profession << endl;
cout << "My age is: " << age << endl;
walk();
talk();
}
void walk() { cout << "I can walk." << endl; }
void talk() { cout << "I can talk." << endl; }
};
int main()
{
MathsTeacher teacher;
teacher.profession = "Teacher";
teacher.age = 23;
teacher.display();
teacher.teachMaths();
Footballer footballer;
footballer.profession = "Footballer";
footballer.age = 19;
footballer.display();
footballer.playFootball();
return 0;
}
Output
My profession is: Teacher
My age is: 23
I can walk.
I can talk.
I can teach Maths.
My profession is: Footballer
30 | P a g e
My age is: 19
I can walk.
I can talk.
I can play Football.
class Perimeter
{
public:
float peri_calc(float l,float b)
{
return 2*(l+b);
}
};
float area_calc()
{
return Area::area_calc(length,breadth);
}
float peri_calc()
{
31 | P a g e
return Perimeter::peri_calc(length,breadth);
}
};
int main()
{
clrscr();
Rectangle r;
r.get_data();
cout<<"Area = "<<r.area_calc();
cout<<"\nPerimeter = "<<r.peri_calc();
getch();
return 0;
}
Output:
Enter length: 4
Enter breadth: 5
Area = 20
Perimeter = 18
int main()
{
clrscr();
B b1;
b1.square();
C c1;
c1.cube();
getch();
}
Output:
#include<iostream.h>
#include<conio.h>
class top
{
public:
int a;
void getdata()
{
cout<<"\n\nEnter first Number :::\t";
cin>>a;
}
void putdata()
{
cout<<"\nFirst Number Is :::\t"<<a;
33 | P a g e
}
};
int main()
{
clrscr();
bottom b1;
b1.cube();
getch();
}
Output:
Enter first Number ::: 23
Square Is :::529
Cube ::: 12167
class B:public A
{
public:
void getc()
{
cout<<"Enter C:";
cin>>c;
}
};
class C
{
public:
void getd()
{
cout<<"Enter D:";
cin>>d;
}
};
void main()
{
clrscr();
D d1;
d1.result();
35 | P a g e
getch();
}
Output:
Enter A:10
Enter B:20
Enter C:30
Enter D:40
Addition is :100
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a[2][3], b[2][3], c[2][3];
int i, j;
getch();
}
Output:
Enter value of A : 10
Enter value of A : 20
Enter value of A : 30
Enter value of A : 40
Enter value of A : 50
Enter value of A : 60
Enter value of B : 10
Enter value of B : 20
Enter value of B : 30
Enter value of B : 40
Enter value of B : 50
Enter value of B : 60
37 | P a g e
Matrix A is :::
10 20 30
40 50 60
Matrix B is :::
10 20 30
40 50 60
Matrix C is :::
20 40 60
80 100 120
void main()
{
clrscr();
int a[2][3], b[3][2];
int i, j;
getch();
}
Output:
Enter value : 10
Enter value : 20
Enter value : 30
Enter value : 40
Enter value : 50
Enter value : 60
Matrix A is :::
10 20 30
40 50 60
Matrix B is :::
10 40
20 50
30 60
void main()
{
clrscr();
int num[10], i, pos = -1, value;
if( pos == -1 )
cout << "\n The element " << value << " not found.";
else
cout << "\n The position of " << value << " is ::: " << pos;
getch();
}
Output:
void main()
40 | P a g e
{
clrscr();
int num[10], i, beg, end, mid, pos = -1, value;
beg = 0;
end = 10 - 1;
while(beg <= end)
{
mid = (beg + end) / 2;
if( value == num[mid] )
{
pos = mid + 1;
break;
}
else if ( value >= num[mid] )
beg = mid + 1;
else
end = mid - 1;
}
if( pos == -1 )
cout << "\n The element " << value << " not found.";
else
cout << "\n The position of " << value << " is ::: " << pos;
getch();
}
Output:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int i, j;
int n[ ] = { 30, 40, 50, 10, 20 };
42 | P a g e
cout << "\n\n After Sorting :::";
for( i=0 ; i<5 ; i++ )
{
cout << " " << n[ i ];
}
getch();
}
Output:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int i, j, small, pos, tmp ;
int n[ ] = { 30, 40, 50, 10, 20 };
getch();
}
Output:
Before Sorting ::: 30 40 50 10 20
After Sorting ::: 10 20 30 40 50
46. Program to explain Insertion Sort
#include <iostream.h>
#include <conio.h>
#include <limits.h>
void main()
{
clrscr();
int i, j, tmp;
int n[ ] = { 0, 30, 40, 50, 10, 20 };
n[0] = INT_MIN;
for( i=1 ; i<=5 ; i++ )
{
tmp = n[ i ];
j = i - 1;
while( tmp < n[ j ] )
44 | P a g e
{
n[ j+1] = n[ j ];
j--;
}
n[ j+1] = tmp;
}
Output:
Before Sorting ::: 30 40 50 10 20
After Sorting ::: 10 20 30 40 50
#include <iostream>
using namespace std;
#include <conio.h>
void merge(int *,int, int , int );
void mergesort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
return;
}
void merge(int *a, int low, int high, int mid)
{
int i, j, k, c[50];
45 | P a g e
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;
i++;
}
while (j <= high)
{
c[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
}
}
int main()
{
int a[20], i, b[20];
cout<<"enter the elements\n";
for (i = 0; i < 5; i++)
{
cin>>a[i];
}
46 | P a g e
mergesort(a, 0, 4);
cout<<"sorted array\n";
for (i = 0; i < 5; i++)
{
cout<<a[i];
}
cout<<"enter the elements\n";
for (i = 0; i < 5; i++)
{
cin>>b[i];
}
mergesort(b, 0, 4);
cout<<"sorted array\n";
for (i = 0; i < 5; i++)
{
cout<<b[i];
}
getch();
}
Output:
enter the elements
78
45
80
32
67
sorted array
32
45
67
78
80
47 | P a g e