Manual - TYL - C++ Training
Manual - TYL - C++ Training
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
}
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
After swapping.
a = 10, b = 5
if(isPrime == true)
cout<<n<<"";
}
return 0;
}
int isPrimeNumber(int n) {
bool isPrime = true;
}
}
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
Output:
lab02@cmrit-ThinkCentre-M57e:~$ g++ gcd.cpp
lab02@cmrit-ThinkCentre-M57e:~$ ./a.out
Enter two numbers12
24
GCD is12
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;
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
}
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
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
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
{
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
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
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
}
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
#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
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
WORD
WODR
WROD
WRDO
WDOR
WDRO
OWRD
OWDR
ORWD
ORDW
ODWR
ODRW
RWOD
RWDO
ROWD
RODW
RDWO
RDOW
DWOR
DWRO
DOWR
DORW
DRWO
DROW
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
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];
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
}
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
589
815
Transpose of Matrix:
158
381
695
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
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
Output:
TYL JULY 2019_L3_C++ Training
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
#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
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
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
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
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
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
{
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
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
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
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
#include<iostream>
using namespace std;
*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
#include<iostream>
using namespace std;
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
#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
{
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
++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
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
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
#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