C++ 4th Sem Pgms
C++ 4th Sem Pgms
1.Write a program to convert given number of days into years, months , week and
days
#include<iostream>
using namespace std;
int main()
{
int y,d,w;
y=d/365;
d=d%365;
w=d/7;
d=d%7;
float totalFahrenheit(float);
float totalCelsius (float);
float main(float)
{
float temp,result;
char choice;
return ((9/5*temp)+(32));
}
return ((temp-32)*(5/9));
}
}
3.Write a program to add two complex numbers using class
#include <iostream>
class complex
{
public :
int real, img;
};
int main()
{
complex a, b, c;
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;
}
4.Write a program to sort array elements in ascending order
#include<iostream.h>
#include<conio.h>
void main()
{
int i,a[10],temp,j;
clrscr();
cout<<"Enter any 10 num in array: \n";
for(i=0;i<=10;i++)
{
cin>>a[i];
}
cout<<"\nData before sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nData after sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
getch();
}
#include<iostream>
using namespace std;
void main()
{
int a[5][5],b[5][5],t[5][5],i,j,k,l,m,n,z,c[5][5],x,y; //declarations
clrscr();
//User inputs
cout<<"enter the order of the matrix > ";
cin>>x;
//Matrix1
cout<<endl<<"enter the 1st matrix elements > ";
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
cin>>a[i][j];
}
}
//Matrix2
cout<<endl<<"enter the 2nd matrix elements > ";
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
cin>>b[i][j];
}
}
//Sum Calculation
cout<<endl<<"SUM...."<<endl;
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
cout<<" "<<a[i][j]+b[i][j];
}
cout<<endl;
}
#include<iostream>
void main()
{
int a[5][5],b[5][5],t[5][5],i,j,k,l,m,n,z,c[5][5],x,y; //declarations
clrscr();
//User inputs
cout<<"enter the order of the 4 matrices > ";
cin>>k;
cin>>l;
cin>>m;
cin>>n;
cout<<endl<<"enter the 1st matrix elements > ";
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
{
cin>>a[i][j];
}
}
cout<<endl<<"enter the 2nd matrix elements > ";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
//Product Calculation
if(l==m)
{
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(z=0;z<l;z++)
{
c[i][j]=c[i][j]+(a[i][z]*b[z][j]);
}
}
} }
else
{
cout<<"invalid data....";
}
//Display Result
cout<<endl<<"PRODUCT..."<<endl;
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
cout<<" "<<c[i][j];
}
cout<<endl;
}
#include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
return 0;
}
8.Write a program to calculate area of circle, rectangle and triangle using function
overloading
#include<iostream>
#include<cstring>
//class definition
class my_string{
private:
//character array to denote string
char str[30];
public:
//function declarations
void getdata();
void display();
void operator== (my_string str1);
int operator= (my_string str1);
void operator+ (my_string str1);
};
void my_string::getdata()
{
cout<<"\nEnter the string : ";
cin>>str;
}
void my_string::display()
{
cout<<"\n"<<str;
}
void my_string::operator== (my_string str1)
{
strcpy(str1.str,str);
cout<<"\n\tCopied String is : "<<str1.str;
}
int my_string::operator<= (my_string str1)
{
if(strcmp(str,str1.str)==0)
return 1; //strings are equal
elseif(strcmp(str,str1.str)<0)
return 2;
else
return 0;
}
void my_string::operator+ (my_string str1)
{
strcat(str,str1.str);
cout<<"\n\t--String After Concat is : "<<str;
}
//start of main function
int main()
{
int opt,c,opt1=1;
//object declaration
my_string a,b;
else
cout<<"\n\t---Strings 1 is larger---\n";
break;
case 2:
a.getdata();
a==b;
break;
case 3:
cout<<"\nEnter the 1st string-\n";
a.getdata();
cout<<"\nEnter the 2nd string-\n";
b.getdata();
a+b;
break;
case 4: return 0;
return 0;
}
10. write a program to create a base class called shape and derive circle and
square from it. Define constructors for all these classes.
#include <iostream>
using namespace std;
class Shape {
protected:
int l;
public:
Shape( int a=0)
{
l = a;
}
int Find_Area()
{
cout << "Parent class area :" <<endl;
return 0;
}
};
class Square: public Shape{
public:
Square( int a=0):Shape(a)
{
}
int Find_Area()
{
cout << " Square class area :" <<endl;
return (l * l);
}
};
class Circle: public Shape{
public:
Circle( int a=0):Shape(a)
{
}
int Find_Area()
{
cout << " Circle class area :" <<endl;
return (3.14*l*l);
}
};
// Main function for the program
int main( )
{
Shape *shape;
Square sqr(10);
Circle cir(5);
shape = &sqr;
shape->Find_Area();
shape = ○
shape->Find_Area();
return 0;
}
11. program to create a base class called vehicle and derive car and bike class
from it
#include<iostream.h>
class Cvehicle
{
protected:
int wheels;
int weight;
int amount;
public:
Cvehicle(int input_wheels, float input_weight, int input_amount)
{
wheels = input_wheels;
weight = input_weight;
amount=input_amount;
}
int get_wheels()
{
return wheels;
}
float get_weight()
{
return weight;
}
int get_amount()
{
return amount;
}
float wheel_load()
{
return (weight/wheels);
}
};
class Ccar : public Cvehicle
{
int passenger_load;
public:
Ccar(int input_wheels, float input_weight, int input_amount ,int people = 4)
{
passenger_load = people;
wheels = input_wheels;
weight = input_weight;
amount=input_amount;
}
int passengers()
{
return passenger_load;
}
};
class Cbike : public Cvehicle
{
int passenger_load;
int mileage;
public:
Cbike(int how_many = 2, int max_mileage = 40)
{
passenger_load = how_many;
mileage = max_mileage;
}
void efficiency(void)
{
Cout<<”mileageis”<<mileage;
}
int passengers(void)
{
return passenger_load;
}
};
void main()
{
Cvehicle unicycle(1,12.5,500000);
cout<<"Unicycle using the Cvehicle base class\n";
cout<<"--------------------------------------\n";
Ccar sedan(4,3500.0,300000,5);
Cbike trail(18,40);
getch();
}
12. To find the m^n of int and float number using function over loading.
#include<iostream.h>
#include<conio.h>
#include<math.h>
//first function
int power(int n, int p=2)
{
int res = pow(n, p);
return res;
}
//second function
double power(double n, int p = 2)
{
double res;
res = pow(n,p);
return res;
}
void main()
{
int value, powr, result;
cout<< "\n Enter a value (any integer ) to find it's square:";
cin>> value;
// first function will be called
result = power(value);
cout<< "\n The square of " << value << " = " << result;
cout<< "\n Enter a value (any integer ) to find it's cube:";
cin>> value;
// first function will be called
result = power(value, 3);
cout<< "\n The cube of " << value < <" = " << result;
cout<< "\n Enter a value (any integer) :";
cin>> value;
cout<< "\n Enter its power (any integer) :";
cin>> powr;
// first function will be called
result = power(value, powr);
cout<< "\n The " << value < <" to the power of " << powr <<" is " << result;
int p;
float val, r;
cout<< "\n Enter a value :";
cin>> val;
cout<< "\n Enter its power ( integer):";
cin>> p;
//second function is called
r = power(val, p);
cout<< "\n The " << val << " to the power of " << p <<" is " << r;
cout<< "\n Enter a value :";
cin>> val;
// second function will be called
r = power(val);
cout<< "\n The square of " << val << " = " << r;
getch();
}
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
float d;
clrscr();
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Enter the value of c:";
cin>>c;
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<"Result is:"<<d;
}
else
{
throw(a-b);
}
}
catch(int i)
{
cout<<"Answer is infinite because a-b is:"<<i;
}
getch();
}
14. To find the mean value of a given number using friend function.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Base with data members and member functions.
STEP 3: The function get() is used to read the 2 inputs from the user.
STEP 4: Declare the friend function mean(base ob) inside the class.
STEP 5: Outside the class to define the friend function and do the following.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class base
int val1,val2;
public:
void get()
cin>>val1>>val2;
};
return float(ob.val1+ob.val2)/2;
}
void main()
clrscr();
base obj;
obj.get();
getch();
15. To count the object value using the storage keyword static.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Stat with data member s and member functions.
STEP 3: The constructor Stat() which is used to increment the value of count as 1 to to assign the
variable code.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class stat
int code;
public:
stat()
{
code=++count;
void showcode()
};
int stat::count;
void main()
clrscr();
stat obj1,obj2;
obj1.showcount();
obj1.showcode();
obj2.showcount();
obj2.showcode();
getch();
16.tower of Hanoi
#include <iostream.h>
#include <conio.h>
void main(){
clrscr();
int n;
cout<<"\n\t\t*****Tower of Hanoi*****\n";
cout<<"\t\tEnter number of discs : ";
cin>>n;
cout<<"\n\n";
tower(n,'A','B','C');
getch();
}
//program to swap the to value of two classes using the friend function
#include <iostream.h>
#include <conio.h>
class Second;
class First
{
private:
int x;
public:
void value1(int);
void display1();
friend void swap(First s1,Second s2); //friend function
};
void First::value1(int a)
{
x=a;
}
void First::display1()
{
cout<<x;
}
class Second
{
private:
int y;
public:
void value2(int);
void display2();
friend void swap(First s1,Second s2); //friend function
};
void Second::value2(int b)
{
y=b;
}
void Second::display2()
{
cout<<y;
}
void swap(First s1,Second s2) //friend function definition
{
int t;
t=s1.x;
s1.x=s2.y;
s2.y=t;
cout<<"Final values of class 1 after swapping (x) = "<<s1.x;
cout<<endl;
cout<<"Final values of class 2 after swapping(y) = "<<s2.y;
cout<<endl;
}
void main()
{
First f;
Second s;
f.value1(9);
s.value2(15);
cout<<"Initial values of class 1 (x) = ";
f.display1();
cout<<endl;
cout<<"Initial values of class 2 (y) = ";
s.display2();
cout<<endl;
swap(f,s); //function call-objects are passed as the parameter
getch();
}
#include<iostream>
using namespace std;
class time
{
int m,s;
public:
void st()
{
cout<< "Enter minutes & seconds"<<endl;
cin>>m>>s; }
void ts()
{
cout<<"Minutes = "<<m<<endl;
cout<<"Seconds = "<<s<<endl; }
friend time operator +(time,time);
};
time operator + (time t1,time t2)
{
time t3;
t3.m=(t1.m+t2.m)+(t1.s+t2.s)/60;
t3.s=(t1.s+t2.s)%60;
return t3;
}
int main()
{
time b1,b2,b3;
b1.st();
b2.st();
b3=b1+b2;
b3.ts();
}
19. Program to store numbers in a file and then read and display odd or even
numbers separately
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int number,i;
clrscr();
f1 = fopen("DATA","r");
f2 = fopen("ODD","w");
f3 = fopen("EVEN","w");
f2 = fopen("ODD","r");
f3 = fopen("EVEN","r");
fclose(f2);
fclose(f3);
getch();
}
20. c++ program to compare age using this pointer
#include <iostream>
class Age
{
public:
// Constructor definition
Age(int h)
{
ag = h;
}
int compare(Age age)
{
return this->ag() > age.h();
}
private:
int ag;
};
int main(void)
{
Age A1(45); // Declare box1
Age A2(30); // Declare box2
if(A1.compare(A2))
{
cout << "A2 is smaller than A1" <<endl;
}
else
{
cout << "A2 is equal to or larger than A1" <<endl;
}
return 0;
}