0% found this document useful (0 votes)
137 views63 pages

Manual - TYL - C++ Training

The document summarizes the topics covered on Day 1 of a C++ training. It includes 13 programs that demonstrate basic C++ concepts like input/output, conditional statements, loops, functions, patterns, strings and more. The programs have sample code and output to illustrate each concept hands-on for the trainees.

Uploaded by

MANGESH KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views63 pages

Manual - TYL - C++ Training

The document summarizes the topics covered on Day 1 of a C++ training. It includes 13 programs that demonstrate basic C++ concepts like input/output, conditional statements, loops, functions, patterns, strings and more. The programs have sample code and output to illustrate each concept hands-on for the trainees.

Uploaded by

MANGESH KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 63

TYL JULY 2019_L3_C++ Training

Day 1: (22/07/19)--BASIC C++ PROGRAMS


Faculty: Prof. Kavitha P, Prof. Sreedevi N and Prof. Prasad B S
1. Write a Simple C++ Program for using input and output (cout and cin)
#include <iostream>
using namespace std;
int main ()
{
int i;
cout <<"Please enter an integer value: ";
cin >> i;
cout <<"The value you entered is "<< i<<"\n";
return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ sample.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Please enter an integer value: 10
The value you entered is 10

2. Write a Program to find the largest of two numbers ( control structure if)
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout<<"Enter first number:";
cin>>num1;
cout<<"Enter second number:";
cin>>num2;
if(num1>num2)
{
cout<<"First number "<<num1<<" is the largest";
}
else
{
TYL JULY 2019_L3_C++ Training

cout<<"Second number "<<num2<<" is the largest";


}
return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ large.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter first number:25
Enter second number:50
Second number 50 is the largest

3. Write a Program to Swap two integer values using temp variable


#include<iostream>
using namespace std;
int main()
{
int a,b,temp;
cout<<"Enter two numbers:\n";
cin>>a>>b;
temp=a;
a=b;
b=temp;
cout<<"swapped numbers:\n"<<a<<""<<b;

}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ swap.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter two numbers:
52
63
swapped numbers:
63 52

4. Write a Program to Swap two integer values without using temp variable
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10;
cout <<"Before swapping."<< endl;
TYL JULY 2019_L3_C++ Training

cout <<"a = "<< a <<", b = "<< b << endl;


a = a + b;
b = a - b;
a = a - b;
cout <<"\nAfter swapping."<< endl;
cout <<"a = "<< a <<", b = "<< b << endl;
return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ swap2.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Before swapping.
a = 5, b = 10

After swapping.
a = 10, b = 5

5. Write a Program to Check if the given number is even or odd


#include <iostream>
using namespace std;
int main()
{
int n;
cout <<"Enter an integer: ";
cin >> n;
if ( n % 2 == 0)
cout << n <<" is even.";
else
cout << n <<" is odd.";
return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ odd.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter an integer: 56
56 is even.lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter an integer: 31
31 is odd.

6. Write a Program to Check if given number is prime or not


#include <iostream>
using namespace std;
int main()
{
int n, i;
bool isPrime = true;
cout <<"Enter a positive integer: ";
cin >> n;
TYL JULY 2019_L3_C++ Training

for(i = 2; i <= n / 2; ++i)


{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout <<"This is a prime number";
else
cout <<"This is not a prime number";
return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ prime.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter a positive integer: 5
This is a prime numberlab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter a positive integer: 6
This is not a prime number

7. Write a Program for generating prime numbers within range 1 to n


#include <iostream>
using namespace std;
int isPrimeNumber(int);
int main() {
bool isPrime;
int count;
cout<<"Enter the value of n:";
cin>>count;
for(int n = 2; n < count; n++)
{
isPrime = isPrimeNumber(n);

if(isPrime == true)
cout<<n<<"";
}
return 0;
}
int isPrimeNumber(int n) {
bool isPrime = true;

for(int i = 2; i <= n/2; i++) {


if (n%i == 0)
{
isPrime = false;
break;
TYL JULY 2019_L3_C++ Training

}
}
return isPrime;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ prime2.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter the value of n:50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

8. Write a Program to implement Euclids algorithm to find the GCD ( two


implementations using mod and subtraction)
#include<iostream>
using namespace std;
int gcd(int m,int n);
int main()
{
int n,m,r;
cin>>n>>m;
r=gcd(m,n);
cout<<r;
}
int gcd(int m,int n)
{
while(m!=n)
{
if(m>n) m=m-n;
else n=m-n;
}
return m;
}

Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ gcd.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter two numbers12
24
GCD is12

9. Write a Program to find the factorial of given number


#include<iostream>
#include<string>
using namespace std;
int Fact(int num)
{
if(num==1)
return 1;
else
TYL JULY 2019_L3_C++ Training

num=num*Fact(num-1);
return num;
}
int main()
{
int n,fact;
cout<<"Enter a number";
cin>>n;
fact=Fact(n);
cout<<"Factorial is "<<fact;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ fact.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter a number5
Factorial is 120

10. Write a Program to print the following patterns ( Discussed multiple patterns with
different level of difficulty)
#include <iostream>
using namespace std;
int main()
{
int rows;
cout <<"Enter number of rows: ";
cin >> rows;

for(int i = 1; i <= rows; ++i)


{
for(int j = 1; j <= i; ++j)
{
cout <<"* ";
}
cout <<"\n";
}
return 0;
}
Output:

lab02@cmrit-ThinkCentre-M57e:~$ g++ pattern1.cpp


lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter number of rows: 4
*
**
***
****
------------------------------------------------------------------------------------------------------------
#include <iostream>
TYL JULY 2019_L3_C++ Training

using namespace std;


int main()
{
int rows, number = 1;

cout <<"Enter number of rows: ";


cin >> rows;

for(int i = 1; i <= rows; i++)


{
for(int j = 1; j <= i; ++j)
{
cout << number <<"";
++number;
}

cout << endl;


}

return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ pat2.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter number of rows: 5
1
23
456
7 8 9 10
11 12 13 14 15

11. Write a Program to convert string from uppercase to lowercase


#include<iostream>
#include<string.h>
using namespace std;
int main()
{ int i;
char c[22];
cin>>c;
for(i=0;i<strlen(c);i++)
cout<<char(*(c+i)+32);

}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ capital.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
CMRIT
Cmrit
TYL JULY 2019_L3_C++ Training

12. Write a program to check if the given number is Armstrong number, unique
number.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a,b,rem,num,sum=0,count=0;
cout<<"Enter the number\n";
cin>>a;
num=b=a;
while(b!=0)
{
b=b/10;
count++;
}
while(a!=0)
{
rem=a%10;
a=a/10;
sum=sum+pow(rem,count);
}
if(num==sum)
cout<<"Armstrong Number\n";
else
cout<<"Not an Armstrong Number\n";
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ arms.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter the number
153
Armstrong Number
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter the number
253
Not an Armstrong Number

13. Word to integer and integer to word conversion


#include<iostream>
using namespace std;
int main()
{
int num,rev=0,rem;
cin>>num;
while(num!=0)
{
TYL JULY 2019_L3_C++ Training

rem=num%10;
num=num/10;
rev=rev*10+rem;
}
while(rem=(rev%10))
{
switch(rem)
{
case 0: cout<<"zero \t";
break;
case 1: cout<<"one \t";
break;
case 2: cout<<"two \t";
break;
case 3: cout<<"three \t";
break;
case 4: cout<<"four \t";
break;
case 5: cout<<"five \t";
break;
case 6: cout<<"six \t";
break;
case 7: cout<<"seven \t";
break;
case 8: cout<<"eight \t";
break;
case 9: cout<<"nine \t";
break;
}
rev=rev/10;
}
}

Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ printnum.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
456
four five six
-----------------------------------------------------------------------------------------------------------

Day 2: (23/07/19)
Faculty: Prof. Kavitha P, Prof. Sreedevi N and Prof. Prasad B S
1. Write a Program to use Break , Continue using arrays
#include <iostream>
using namespace std;
TYL JULY 2019_L3_C++ Training

int main() {
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
cout<<i<<"\n";
}
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ break.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
1
2
3
4
---------------------------------------------------------------------------------------------------------

#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10; ++i)
{
if ( i == 6 || i == 9)
{
continue;
}
TYL JULY 2019_L3_C++ Training

cout << i <<"\t";


}
return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ continue.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
1 2 3 4 5 7 8 10

2. Write a Program to find Average of array elements


#include<iostream>
using namespace std;
int main()
{
int n,i,sum=0,avg=0;
int a[50];
cout<<"Enter n:\n";
cin>>n;
cout<<"Enter numbers\n";
for(i=0;i<n;i++)
{
cin>>a[i];
if(a[i]<0)
break;
sum+=a[i];
}
avg=sum/n;
if(n==i)
cout<<”Average is “<<avg;
}
TYL JULY 2019_L3_C++ Training

Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ avg.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter n:
5
Enter numbers
6
3
9
2
3
Average is4

3. Write a Program for Sorting of array elements ( multiple algorithms)


#include<iostream>
using namespace std;
int main()
{
int n,temp,i,j;
int a[50];
cout<<"Enter n:\n";
cin>>n;
cout<<"Enter elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j+1]<a[j])
TYL JULY 2019_L3_C++ Training

{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
cout<<”After sorting”<<a[i]<<"";
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ sort.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter n:6 Enter elements:
9
6
3
2
1
7
After sorting1 2 3 6 7 9

4. Write a Program for implementing Liner Search and Binary Search


#include<iostream>
using namespace std;
int main()
{
int a[50];
int n,i,pos,search,flag=0;
cout<<"Enter n:\n";
TYL JULY 2019_L3_C++ Training

cin>>n;
cout<<"Enter elemets:\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the search element:\n";
cin>>search;
for(i=0;i<n;i++)
{
if(a[i]==search)
{ pos=i;
flag=1;
}
}
if(flag==1)
cout<<"Element found at pos "<<pos+1;
else
cout<<"Element not found";
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ search.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter n:
6
Enter elemets:
6
9
3
2
1
7
TYL JULY 2019_L3_C++ Training

Enter the search element:


5
Element not foundlab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter n:
6
Enter elemets:
6
9
3
2
1
7
Enter the search element:
1
Element found at pos 5
---------------------------------------------------------------------------------------------------
//binary search
#include<iostream>
using namespace std;
int main()
{
int a[50],flag=0;
int n,i,j,pos,search,low,high,mid,temp;
cout<<"Enter n:\n";
cin>>n;
cout<<"Enter elemets:\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n-1;i++)
{
TYL JULY 2019_L3_C++ Training

for(j=0;j<n-i-1;j++)
{
if(a[j+1]<a[j])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"Enter search element:\n";
cin>>search;
mid=n/2;
high=n;
low=0;
while(low<=high)
{
mid=(low+high)/2;
if(search==a[mid]){
pos=mid;
flag=1;
break; }
else if(search<a[mid])
high=mid-1;
else
low=mid+1;
}
if(flag==1)
cout<<"Element found at pos "<<pos;
else
TYL JULY 2019_L3_C++ Training

cout<<"Element not found";

}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ binarys.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter n:
6
Enter elements:
8
9
6
1
2
3
Enter search element:
9
Element found at pos 5lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter n:
3
Enter elements:
6
3
2
Enter search element:
5
Element not found

5. Write a Program for Palindrome and Calindrome ( Numbers and Strings)


#include<iostream>
TYL JULY 2019_L3_C++ Training

using namespace std;


int main()
{
int a,num,rem,rev=0;
cout<<"Enter the number\n";
cin>>num;
a=num;
while(a!=0)
{
rem=a%10;
a=a/10;
rev=rev*10+rem;
}
if(num==rev)
cout<<"Palindrome Number\n";
else
cout<<"Not a Palindrome Number\n";
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ palin.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter the number
565
Palindrome Number
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter the number
231
Not a Palindrome Number
-----------------------------------------------------------------------------------------------
//String Palindrome
TYL JULY 2019_L3_C++ Training

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str[50];
int i,n,flag=0;
cout<<"Enter the string:\n";
cin>>str;
n=strlen(str);
for(i=0;i<n;i++)
{
if(str[i]!=str[n-i-1])
{ flag=1;
break;
}
}
if(flag==1)
cout<<"Not a palindrome\n";
else
cout<<"Palindrome\n";
}

Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ strpalin.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter the string:
mom
Palindrome
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
TYL JULY 2019_L3_C++ Training

Enter the string:


mummy
Not a palindrome

6. Write a Program for Word and Number scrambling ( all permutation using iterative
and recursive)
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int i,j,k,l;
char s[10]="WORD";
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
for(l=0;l<4;l++)
{
if(i==k||i==j||i==l||j==k||j==l||k==l)
continue;
cout<<s[i]<<s[j]<<s[k]<<s[l]<<endl;
}
}
}
}
}
Output:
TYL JULY 2019_L3_C++ Training

lab02@cmrit-ThinkCentre-M57e:~$ g++ word.cpp


lab02@cmrit-ThinkCentre-M57e:~$ ./a.out

WORD
WODR
WROD
WRDO
WDOR
WDRO
OWRD
OWDR
ORWD
ORDW
ODWR
ODRW
RWOD
RWDO
ROWD
RODW
RDWO
RDOW
DWOR
DWRO
DOWR
DORW
DRWO
DROW

7. Write a Program to Reverse of a string without using temporary array


#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50], temp;
int i, j;
cout <<"Enter a string : ";
cin>>str;
j = strlen(str) - 1;
for (i = 0; i < j; i++,j--)
{
TYL JULY 2019_L3_C++ Training

temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout <<"\nReverse string : "<< str;
return 0;
}

Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ strrev.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter a string : hello
Reverse string : olleh

8. Write a Program to Remove all occurrence of specific character from a string


#include <iostream>
#include <string>
#include<algorithm>
using namespace std;
int main()
{
string s;
char c;
cout <<"Enter the string : ";
cin >> s;
cout <<"\nEnter the character : ";
cin >> c;
s.erase(remove(s.begin(), s.end(), c), s.end());
cout <<"\nString after removing the character "<< c <<" : "<< s;
}
TYL JULY 2019_L3_C++ Training

Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ remove.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter the string : hello
Enter the character : l
String after removing the character l : heo

9. Write a Program to find Sum of diagonal, upper triangle, lower triangle elements of
matrix
#include<iostream>
using namespace std;
int main()
{
int arr[5][5],a=0,b=0,i,j,n;
cout<<"Enter size of matrix(max 5):";
cin>>n;
cout<<"Enter the matrix:\t";

for(i=0;i<n;++i)
for(j=0;j<n;++j)
cin>>arr[i][j];

for(i=0;i<n;++i)
for(j=0;j<n;++j)
if(j>i)
a+=arr[i][j];
else
if(i>j)
b+=arr[i][j];

cout<<"\nSum of upper triangle"<<a;


cout<<"\nSum of Lower triangle:"<<b;
return 0;
}

Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ diag.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter size of matrix(max 5):3
Enter the matrix: 5
6
TYL JULY 2019_L3_C++ Training

3
3
3
3
3
3
3

Sum of upper triangle12


Sum of Lower triangle:9

10. Write a Program to find Transpose of a matrix.


#include <iostream>
using namespace std;
int main()
{
int a[10][10], trans[10][10], r, c, i, j;
cout <<"Enter rows and columns of matrix: ";
cin >> r >> c;
cout << endl <<"Enter elements of matrix: "<< endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout <<"Enter elements a"<< i + 1 << j + 1 <<": ";
cin >> a[i][j];
}
cout << endl <<"Entered Matrix: "<< endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout <<""<< a[i][j];
if(j == c - 1)
cout << endl << endl;
TYL JULY 2019_L3_C++ Training

}
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
trans[j][i]=a[i][j];
}
cout << endl <<"Transpose of Matrix: "<< endl;
for(i = 0; i < c; ++i)
for(j = 0; j < r; ++j)
{
cout <<""<< trans[i][j];
if(j == r - 1)
cout << endl << endl;
}
return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ mattrs.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter rows and columns of matrix: 3
3
Enter elements of matrix:
Enter elements a11: 1
Enter elements a12: 3
Enter elements a13: 6
Enter elements a21: 5
Enter elements a22: 8
Enter elements a23: 9
Enter elements a31: 8
Enter elements a32: 1
TYL JULY 2019_L3_C++ Training

Enter elements a33: 5


Entered Matrix:
136

589

815

Transpose of Matrix:
158

381

695

11. Write a Program to find Sum of series ( Multiple type of series)


#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double sum = 0, a;
int n, i;
cout <<" Input the value for nth term: ";
cin >> n;
for (i = 1; i <= n; ++i)
{
a = 1 / pow(i, i);
cout <<"1/"<< i <<"^"<< i <<" = "<< a << endl;
TYL JULY 2019_L3_C++ Training

sum += a;
}
cout <<" The sum of the above series is: "<< sum << endl;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ series.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Input the value for nth term: 6
1/1^1 = 1
1/2^2 = 0.25
1/3^3 = 0.037037
1/4^4 = 0.00390625
1/5^5 = 0.00032
1/6^6 = 2.14335e-05
The sum of the above series is: 1.29128

12. Write a Program to find reverse of a given number


#include<iostream>
using namespace std;
int main()
{
int num,rev=0,rem,mul=1;
cin>>num;

while(num!=0)
{
rem=num%10;
num=num/10;
rev=rev*10+rem;
TYL JULY 2019_L3_C++ Training

}
cout<<rev<<endl;

}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ numrev.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter a number456
Reverse number is654

13. Write a Program to find Sum and product of digits in a number


//Product
#include <iostream>
using namespace std;
int main()
{
int num1, num2, r, pro=1,i;
cout <<" Input a number: ";
cin >> num1;
num2 = num1;
for(i=num1;i>0;i=i/10)
{
r = i % 10;
pro = pro*r;
}
cout <<" The product of "<< num2 <<" is: "<< pro << endl;
}

Output:
TYL JULY 2019_L3_C++ Training

lab02@cmrit-ThinkCentre-M57e:~$ g++ pro.cpp


lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Input a number: 563
The product of 563 is: 90
----------------------------------------------------------------------------------------------
//sum
#include <iostream>
using namespace std;
int sumDigits(int num1,int n)
{
int sum = 0;
while (num1 != 0)
{
sum += num1 % 10;
num1 /= 10;
}
return sum;
}
int main()
{
int num1,sum,n;
sum=0;
cout <<" Input any number: ";
cin>> num1;
n=num1;
cout<<" The sum of the digits "<<n<<"is:"<< sumDigits(num1,n) <<endl;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ sum.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
TYL JULY 2019_L3_C++ Training

Input any number: 852


The sum of the digits 852is:15

Day 3: (24/07/19)
Faculty: Prof. Kavitha P, Prof. Sreedevi N and Prof. Prasad B S
Programs:
1. Write a Program to eliminate duplicates in an array
#include<iostream>
using namespace std;
int main()
{
int a[10];
int i,j,n;
cout<<"Enter n:";
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
a[j]=a[j+1];
n--;
}
}
cout<<a[i];
}
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ dup.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter n:5
6
3
2
1
3
6321

2. Write a Program for counting the frequency of elements in the array


TYL JULY 2019_L3_C++ Training

#include <iostream>
using namespace std;
int main()
{
int freq[100];
int size, i, j, count;
cout <<"\nEnter size of array: ";
cin >> size;
int arr[size];
cout <<"\nEnter elements in array: ";
for(i=0; i<size; i++)
{
cin >> arr[i];
freq[i] = -1;
}
for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
if(arr[i]==arr[j])
{
count++;
freq[j] = 0;
}
}
if(freq[i] != 0)
{
freq[i] = count;
}
TYL JULY 2019_L3_C++ Training

}
cout <<"\nFrequency of all elements of array : \n";
for(i=0; i<size; i++)
{
if(freq[i] != 0)
{
cout << arr[i] <<" occurs "<< freq[i] <<"times "<< endl;
}
}
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ freq.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter size of array: 5
Enter elements in array: 6
3
2
3
1
Frequency of all elements of array :
6 occurs 1times
3 occurs 2times
2 occurs 1times
1 occurs 1times

3. Write a Program for printing the year , month and day for the given integer(days)
#include<iostream>
using namespace std;
int main()
{
int n,year,month,days;
cout<<"Enter number:\n";
TYL JULY 2019_L3_C++ Training

cin>>n;
year=n/365;
days=n%365;
if(days>30)
{
month=days/30;
days=days-(month*30);
}
cout<<year<<" years "<<month<<" months "<<days<<" days ";

}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ year.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter number:
568
1 years 6 months 23 days
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter number:
7953
21 years 9 months 18 days

4. Write a Program using class for reading and printing the student details
#include<iostream>
using namespace std;
class student
{
private:
int usn,total,marks[3];
char name[10];
float per;
public:
int i;
student() : total(0),per(0.0){}
void getdata()
{
cout<<"Enter USN:\n";
cin>>usn;
cout<<"Enter Name:\n";
cin>>name;
cout<<"Enter marks for 3 subjects:\n";
for(i=0;i<3;i++)
cin>>marks[i];
}

void display()
{
cout<<"\nUSN:"<<usn<<endl;
cout<<"Name:"<<name<<endl;
TYL JULY 2019_L3_C++ Training

cout<<"Marks are:\n";
for(i=0;i<3;i++)
cout<<marks[i]<<"";
cout<<"\nTotal Marks:"<<total<<endl;
cout<<"Percentage:"<<per<<endl;
}
void calculate()
{
for(i=0;i<3;i++)
total+=marks[i];
per=(total/3);
}
};
int main()
{
student s1,s2;
s1.getdata();
s2.getdata();
s1.calculate();
s2.calculate();
s1.display();
s2.display();
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ student.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter USN:
10
Enter Name:
raju
Enter marks for 3 subjects:
85
78
63
Enter USN:
15
Enter Name:
mani
Enter marks for 3 subjects:
65
63
64

USN:10
Name:raju
Marks are:
85 78 63
Total Marks:226
Percentage:75

USN:15
TYL JULY 2019_L3_C++ Training

Name:mani
Marks are:
65 63 64
Total Marks:192
Percentage:64

5. Write a Program for Arithmetic operations using Inline Function


#include<iostream>
using namespace std;
class operation
{
private:
int a,b;
public:
void get();
int addition();
int difference();
int product();
float division();
};

inline void operation::get()


{
cout<<"Enter values:\n";
cin>>a>>b;
}

inline int operation::addition()


{
return (a+b);
}

inline int operation::difference()


{
return (a-b);
}

inline int operation::product()


{
return (a*b);
}

inline float operation::division()


{
return (a/b);
}

int main()
{
int i;
TYL JULY 2019_L3_C++ Training

operation o[4];
for(i=0;i<4;i++)
{
cout<<"\niteration "<<i<<endl;
o[i].get();
cout<<"Addition "<<o[i].addition()<<endl;
cout<<"Difference "<<o[i].difference()<<endl;
cout<<"Product "<<o[i].product()<<endl;
cout<<"Division "<<o[i].division()<<endl;
}
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ arith.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
iteration 0
Enter values:
25
3
Addition 28
Difference 22
Product 75
Division 8
iteration 1
Enter values:
3
3
Addition 6
Difference 0
Product 9
Division 1
iteration 2
Enter values:

6. Write a Program for swapping two values using call by value and call by reference
#include <iostream>
using namespace std;
void swap(int&, int&);
int main()
{
int a = 1, b = 2;
cout <<"Before swapping"<< endl;
cout <<"a = "<< a << endl;
cout <<"b = "<< b << endl;
TYL JULY 2019_L3_C++ Training

swap(a, b);
cout <<"\nAfter swapping"<< endl;
cout <<"a = "<< a << endl;
cout <<"b = "<< b << endl;
return 0;
}
void swap(int& n1, int& n2) {
int temp;
temp = n1;
n1 = n2;
n2 = temp;
}

Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ pswap.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Before swapping
a=1
b=2

After swapping
a=2
b=1

7. Write a Program for passing of 1D and 2D arrays


#include <iostream>
using namespace std;
void display(int marks[5]);
int main()
{
TYL JULY 2019_L3_C++ Training

int marks[5] = {88, 76, 90, 61, 69};


display(marks);
return 0;
}
void display(int m[5])
{
cout <<"Displaying marks: "<< endl;
for (int i = 0; i < 5; ++i)
{
cout <<"Student "<< i + 1 <<": "<< m[i] << endl;
}
}

Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ 1d.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Displaying marks:
Student 1: 88
Student 2: 76
Student 3: 90
Student 4: 61
Student 5: 69
---------------------------------------------------------------------------------------------------
//2D
#include <iostream>
using namespace std;
void display(int n[3][2]);
int main()
{
int num[3][2] = {
TYL JULY 2019_L3_C++ Training

{3, 4},
{9, 5},
{7, 1}
};
display(num);
return 0;
}
void display(int n[3][2])
{
cout <<"Displaying Values: "<< endl;
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 2; ++j)
{
cout << n[i][j] <<"";
}
}
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ 2d.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Displaying Values:
349571

8. Write a Program for passing of objects


#include <iostream>
using namespace std;
class Complex
{
private:
TYL JULY 2019_L3_C++ Training

int real;
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=comp1.real+comp2.real;

imag=comp1.imag+comp2.imag;
}
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;
}
TYL JULY 2019_L3_C++ Training

Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ passing.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter real and imaginary number respectively:
5
6
Enter real and imaginary number respectively:
5
6
Sum = 10+12i

9. Write a Program for demonstrating Storage Classes


#include<iostream>
using namespace std;
static int cnt=5;
void test();
int main()
{
while(cnt--)
test();
return 0;
}
void test()
{
static int i=10;
i++;
cout<<"i= "<<i<<" cnt= "<<cnt<<endl;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ static.cpp
TYL JULY 2019_L3_C++ Training

lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
i= 11 cnt= 4
i= 12 cnt= 3
i= 13 cnt= 2
i= 14 cnt= 1
i= 15 cnt= 0

10. Write a Program to illustrate usage of pointers, reference and dereferencing


#include<iostream>
using namespace std;
int main()
{
int a=5,*p=0;
char c='a',*q;
float f=12.4,*r;
void *s;
cout<<"p= "<<p<<endl;
p=&a;
q=&c;
r=&f;
cout<<"a= "<<a<<endl;
cout<<"p= "<<p<<endl;
cout<<"*p= "<<*p<<endl;
cout<<"&a= "<<&a<<endl;
cout<<"&p= "<<&p<<endl;
cout<<"\n";
cout<<"c= "<<c<<endl;
cout<<"q= "<<q<<endl;
cout<<"*q= "<<*q<<endl;
cout<<"&c= "<<&c<<endl;
cout<<"&q= "<<&q<<endl;
cout<<"\n";
cout<<"f= "<<f<<endl;
cout<<"r= "<<r<<endl;
cout<<"*r= "<<*r<<endl;
cout<<"&f= "<<&f<<endl;
cout<<"&r= "<<&r<<endl;
int b=7;
s=&b;
cout<<*(int*)s;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ pointer.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
p= 0
TYL JULY 2019_L3_C++ Training

a= 5
p= 0xbf9c3520
*p= 5
&a= 0xbf9c3520
&p= 0xbf9c3524
c= a
q= a
*q= a
&c= a
&q= 0xbf9c3528
f= 12.4
r= 0xbf9c352c
*r= 12.4
&f= 0xbf9c352c
&r= 0xbf9c3530

11. Write a Program to swap elements using pointers


#include<iostream>
using namespace std;
int main() {
int x, y, temp;
int *a, *b;
cout <<"Enter two Numbers :";
cin >> x>>y;
a = &x;
b = &y;
temp = *a;
*a = *b;
*b = temp;
cout <<"\nAfter Swap x is :"<< x;
cout <<"\nAfter Swap y is :"<< y;
return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ sp.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter two Numbers :5
2
After Swap x is :2
After Swap y is :5

12. Write a Program to reverse a string using pointers


#include<iostream>
using namespace std;
int main()
TYL JULY 2019_L3_C++ Training

{
char str[100],res[100];
int i=0;
char *strp=str;
char *resp=res;
cout<<"Enter a string :- ";
cin>>str;
while(*strp)
{
strp++;
i++;
}
while(i>0)
{
strp--;
*resp=*strp;
resp++;
--i;
}
*resp='\0';
cout<<"Reverse of a string is:- "<<res;

}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ revstr.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter a string :- cmrit
Reverse of a string is:- tirmc
TYL JULY 2019_L3_C++ Training

13. Write a Program for implementing function calling by reference using pointer and
using reference variable
#include<iostream>
using namespace std;
void swap1(int *a,int *b);
void swap(int a,int b);
int main()
{
int x,y;
cin>>x>>y;
cout<<"Before swap x="<<x<<" y="<<y<<endl;
swap(x,y);
cout<<"After swap with value x="<<x<<" y="<<y<<endl;
swap1(&x,&y);
cout<<"After swap with reference x="<<x<<" y="<<y<<endl;
}
void swap1(int *a,int *b)
{
*b=*a+*b;
*a=*b-*a;
*b=*b-*a;
}
void swap(int a,int b)
{
b=a+b;
a=b-a;
b=b-a;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ refswap.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
52
32
Before swap x=52 y=32
After swap with value x=52 y=32
After swap with reference x=32 y=52

14. Write a Program to illustrate multidimensional array access using pointers


#include<iostream>
using namespace std;
int main()
{
int a[5]={1,2,3,4,5};
int b[2][2]={6,7,8,9};
TYL JULY 2019_L3_C++ Training

int *p;
int (*q)[2];
p=a; //p=&a[0]
q=b;
cout<<*(p)<<""<<*(p+1)<<""<<*(p+2)<<""<<*(p+3)<<""<<*(p+4)<<""<<endl;
cout<<*(*(q))<<""<<*(*(q)+1)<<""<<*(*(q+1))<<""<<*(*(q+1)+1)<<""<<endl;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ printpoint.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
12345
6789

15. Write a Program to illustrate new and delete to create variables and objects
dynamically
#include <iostream>
using namespace std;
class Test
{
private:
int num;
float *ptr;
public:
Test()
{
cout <<"Enter total number of students: ";
cin >> num;
ptr = new float[num];
cout <<"Enter GPA of students."<< endl;
for (int i = 0; i < num; ++i)
{
cout <<"Student"<< i + 1 <<": ";
cin >> *(ptr + i);
}
}
~Test() {
delete[] ptr;
}
void Display() {
TYL JULY 2019_L3_C++ Training

cout <<"\nDisplaying GPA of students."<< endl;


for (int i = 0; i < num; ++i) {
cout <<"Student"<< i+1 <<" :"<< *(ptr + i) << endl;
}
}
};
int main() {
Test s;
s.Display();
return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ new.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter total number of students: 5
Enter GPA of students.
Student1: 63
Student2: 62
Student3: 80
Student4: 90
Student5: 52
Displaying GPA of students.
Student1 :63
Student2 :62
Student3 :80
Student4 :90
Student5 :52
------------------------------------------------------------------------------------------------------------
Day 4: (25/07/19) Faculty: Prof. Kavitha P, Prof. Sreedevi N and Prof. Prasad B S
1. Write a Program to create a distance class and to add 2 distances with feet and
inches as input(1feet=12inches)
#include<iostream>
using namespace std;
class distanceadd
{
private:
int inch,feet;
public:
void getdist()
{
cout<<"Enter feet and inch:";
TYL JULY 2019_L3_C++ Training

cin>>feet>>inch;
}
void adddist(distanceadd d1,distanceadd d2)
{
feet=d1.feet+d2.feet;
inch=d1.inch+d2.inch;
while(inch>12)
{
inch=inch-12;
feet=feet+1;
}
cout<<"distance-"<<feet<<"'"<<inch<<"''";
}
};
int main()
{
distanceadd d1,d2,d3;
d1.getdist();
d2.getdist();
d3.adddist(d1,d2);
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ dist.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter feet and inch:5
10
Enter feet and inch:6
13
distance-12'11''
TYL JULY 2019_L3_C++ Training

2. Write a Program to create time class with hours,min,sec as members and add 2 time
objects and print the result in HH:MM:SS
#include<iostream>
using namespace std;
class timeadd
{
private:
int hr,m,s;
public:
void gettime()
{
cout<<"Enter Time in format of HH:MM:SS:\n";
cin>>hr>>m>>s;
}
void addtime(timeadd t1,timeadd t2)
{
hr=t1.hr+t2.hr;
m=t1.m+t2.m;
s=t1.s+t2.s;
if(s>60)
{
s=s-60;
m=m+1;
}
if(m>60)
{
m=m-60;
hr=hr+1;
}
cout<<"Total time is:\n";
TYL JULY 2019_L3_C++ Training

cout<<hr<<":"<<m<<":"<<s;
}
};
int main()
{
timeadd t1,t2,t3;
t1.gettime();
t2.gettime();
t3.addtime(t1,t2);
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ time.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter Time in format of HH:MM:SS:
5
25
36
Enter Time in format of HH:MM:SS:
6
38
36
Total time is:
12:4:12

3. Create a class for employee and prepare a pay bill. The input is the basic pay
DA=52% of Basic, IT = 30% of gross
Gross=Basic pay + 52% of basic pay) and Net pay= gross – 30% of gross
Print the net pay as a result for all employees (At least read 10 Employee details)
#include<iostream>
using namespace std;
TYL JULY 2019_L3_C++ Training

class employee
{
private:
int pay;
float DA,IT,GP,NP;
public:
void getpay()
{
cout<<"Enter basic pay:\n";
cin>>pay;
}
void calculate()
{
DA=0.52*pay;
GP=DA+pay;
IT=0.3*GP;
NP=GP-IT;
cout<<"Net pay is:"<<NP;
}
};
int main()
{
employee e1;
e1.getpay();
e1.calculate();
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ employee.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter basic pay:
TYL JULY 2019_L3_C++ Training

12000
Net pay is:12768

4. Write a Program to illustrate template function to add two variables of integer and
float type
#include<iostream>
using namespace std;
template <class T>
T add(T a,T b)
{
T sum;
sum=a+b;
return sum;
}
int main()
{
int a=5,b=8;
float c=12.5,d=20.4;
a=add(a,b);
c=add(c,d);
cout<<a<<""<<c;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ template.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
13 32.9

5. Write a Program to find the largest of two integer , float and char using template
function
#include<iostream>
using namespace std;
TYL JULY 2019_L3_C++ Training

template <class T>

T large(T a, T b)
{
if(a>b)
return a;
else
return b;
}

int main()
{
int a,b;
float c,d;
char e,f;
cout<<"enter numbers of int to find large";
cin>>a>>b;
cout<<"enter numbers of float to find large";
cin>>c>>d;
cout<<"enter char to find large";
cin>>e>>f;
a=large(a,b);
c=large(c,d);
e=large(e,f);
cout<<a<<""<<c<<""<<e;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ templarge.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
enter numbers of int to find large5
6
enter numbers of float to find large2.0
3.5
enter char to find largeh
a
6 3.5 h

6. Write a Program to swap two elements using template function

#include<iostream>
using namespace std;

template <class T>

T swap(T *a,T *b)


{
T temp;
temp=*b;
TYL JULY 2019_L3_C++ Training

*b=*a;
*a=temp;
}

int main()
{
int a=5,b=6;
float c=12.4,d=23.4;
char e='A',f='B';
cout<<"Before swap:\n";
cout<<a<<""<<b<<endl;
cout<<c<<""<<d<<endl;
cout<<e<<""<<f<<endl;
cout<<"After swap:\n";
swap(&a,&b);
swap(&c,&d);
swap(&e,&f);
cout<<a<<""<<b<<endl;
cout<<c<<""<<d<<endl;
cout<<e<<""<<f<<endl;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ tempswap.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Before swap:
56
12.4 23.4
AB
After swap:
65
23.4 12.4
BA

7. Write a template class calculator to perform addition , multiplication , subtraction


on int , float and double data types

#include<iostream>
using namespace std;

template <class T>

class calculator
{
//private: T a,b;
public:
T add(T a,T b)
{
return (a+b);
TYL JULY 2019_L3_C++ Training

}
T sub(T a,T b)
{
return (a-b);
}
T mul(T a,T b)
{
return (a*b);
}
};

int main()
{
calculator <int> c1;
calculator <float> c2;
calculator <double> c3;
int a,b;
float c,d;
double e,f;
cout<<”Enter the numbers to perform operation”;
cin>>a>>b>>c>>d>>e>>f;

cout<<c1.add(a,b)<<"";
cout<<c2.add(c,d)<<"";
cout<<c3.add(e,f)<<endl;

cout<<c1.sub(a,b)<<"";
cout<<c2.sub(c,d)<<"";
cout<<c3.sub(e,f)<<endl;

cout<<c1.mul(a,b)<<"";
cout<<c2.mul(c,d)<<"";
cout<<c3.mul(e,f)<<"";

}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ tempclass.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter the numbers to perform operation5
6
9
7
2
1
Addition11 16 3
Subtraction-1 2 1
Multiplication30 63 2

8. Write a Program to demonstrate use of friend function


TYL JULY 2019_L3_C++ Training

#include<iostream>
#include<exception>
using namespace std;
class B;

class A
{
public:
void getb(B &);
};
class B
{
private:
int b;
public:
B() { b=0; }
friend void A::getb(B &);
};
void A::getb(B &x)
{
cout<<x.b;
}
int main()
{
A a;
B x;
a.getb(x);
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ friend.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
0

9. Write a Program to demonstrate function overloading(area of shapes)


#include<iostream>
#include<exception>
using namespace std;
class A
{ private:
float pi;
public:
A(){ pi=3.14; }
float area(int r)
TYL JULY 2019_L3_C++ Training

{
return (pi*r*r);
}
int area(int l,int h)
{
return (l*h);
}
};
int main()
{
A a;
int rad,len,ht;
cout<<"Radius:\n";
cin>>rad;
cout<<"Length and height:\n";
cin>>len>>ht;
cout<<"Area of circle "<<a.area(rad)<<endl;
cout<<"Area of rectangle "<<a.area(len,ht);

}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ fnoverload.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Radius:
25
Length and height:
6
3
Area of circle 1962.5
Area of rectangle 18
TYL JULY 2019_L3_C++ Training

10. Write a Program to demonstrate operator overloading(overload +,- operator)


#include <iostream>
using namespace std;
class Test
{
private:
int count;
public:
Test(): count(5){}
void operator ++()
{
count = count+1;
}
void Display() { cout<<"Count: "<<count; }
};
int main()
{
Test t;

++t;
t.Display();
return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ opoverload.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Count: 6
TYL JULY 2019_L3_C++ Training

11. Write a Program to demonstrate overloading of [ ] operator


#include<iostream>
#include<cstdlib>
using namespace std;
class Array
{
private:
int *ptr;
int size;
public:
Array(int *, int);
int &operator[] (int);
void print() const;
};
int &Array::operator[](int index)
{
if (index >= size)
{
cout <<"Array index out of bound, exiting";
exit(0);
}
return ptr[index];
}
Array::Array(int *p = NULL, int s = 0)
{
size = s;
ptr = NULL;
if (s != 0)
{
TYL JULY 2019_L3_C++ Training

ptr = new int[s];


for (int i = 0; i < s; i++)
ptr[i] = p[i];
}
}
void Array::print() const
{
for(int i = 0; i < size; i++)
cout<<ptr[i]<<"";
cout<<endl;
}
int main()
{
int a[] = {1, 2, 4, 5};
Array arr1(a, 4);
arr1[2] = 6;
arr1.print();
arr1[8] = 6;
return 0;
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ opover.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
1265
Array index out of bound, exiting

12. Write a Program to handle divide by zero exception ( user handling and standard
exception handling)
#include<iostream>
#include<exception>
TYL JULY 2019_L3_C++ Training

using namespace std;

int divide(int a,int b)


{
return a/b;
}

int main()
{
int a,b,d;
cin>>a>>b;
try{
d=divide(a,b);
cout<<d;
}catch(const char* msg)
{
cout<<msg;
}

}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ divexcp.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
6
3
2lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
9
6
1
TYL JULY 2019_L3_C++ Training

13. Write a Program to handle array index out of bound exception

#include<iostream>
#include<exception>
using namespace std;
int main()
{
int n,index,i,flag=0,a[10];
cout<<"enter number";
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
while(flag!=1)
{
cin>>index;
try{
if(index>=n)
{
flag=1;
throw "Array Index out of range";
}
else
cout<<a[index];
}catch(const char* msg)
{
cout<<msg;
}
TYL JULY 2019_L3_C++ Training

}
}
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ arrayexcp.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
enter number5
3
2
1
3
6
5
Array Index out of range

You might also like