Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ File Solution

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 51

#include <iostream>

#define N 10
using namespace std;
int main()
{
int i, MAX, MIN;
int arr[N];
cout << "Enter the 10 Elements\n";
for (i = 0; i < N; i++)
{
cin >> arr[i];
}
cout << "You Entered :\n";
for (i = 0; i < N; i++)
{
cout << arr[i] << " ";
}
MAX = MIN = arr[0];
for (i = 0; i < N; i++)
{
if (arr[i] < MIN)
{
MIN = arr[i];
}
if (arr[i] > MAX)
{
MAX = arr[i];
}
}
cout << "\nMaximum Element in
Array:" << MAX << endl;
cout << "Minimum Element in
Array:" << MIN << endl;
return 0;
}
Output-:Enter the 10 Elements
1 2 3 4 5 6 7 8 9 10
You Entered :
1 2 3 4 5 6 7 8 9 10
Maximum Element in Array:10
Minimum Element in Array:1
2. #include <iostream>
using namespace std;
class Demo
{
private:
int a, b;
public:
int Add(int x, int y)
{
a = x;
b = y;
return a + b;
}
int Sub(int x, int y)
{
a = x;
b = y;
return (a - b);
}
int Mul(int x, int y)
{
a = x;
b = y;
return (a * b);
}
float Div(int x, int y)
{
a = x;
b = y;
return (a / b);
}
};
int main()
{
int m, n;
cout << "Enter to Number to perform Arithmetic
Operation:\n";
cin >> m >> n;
Demo d;
cout << "Addition of " << m << " and " << n << "is:" <<
d.Add(m, n) << endl;
cout << "Substraction of " << m << " and " << n <<
"is:" << d.Sub(m, n) << endl;
cout << "Multiplication of " << m << " and " << n <<
"is:" << d.Mul(m, n) << endl;
cout << "Division of " << m << " and " << n << "is:" <<
d.Div(m, n) << endl;
;
return 0;
}
Output-:Enter to Number to perform Arithmetic
Operation:
40 20
Addition of 40 and 20is:60
Substraction of 40 and 20is:20
Multiplication of 40 and 20is:800
Division of 40 and 20is:2
3. #include <iostream>
using namespace std;
class Demo
{
private:
int a, b;

public:
int Add(int &x, int &y)
{
a = x;
b = y;
return a + b;
}
int Sub(int &x, int &y)
{
a = x;
b = y;
return (a - b);
}
int Mul(int &x, int &y)
{
a = x;
b = y;
return (a * b);
}
float Div(int &x, int &y)
{
a = x;
b = y;
return (a / b);
}
float Avg(int &x, int &y)
{
a = x;
b = y;
float res = (a + b);
return (res / 2.0);
}
};
int main()
{
int m, n;
cout << "Enter to Number to perform Arithmetic
Operation:\n";
cin >> m >> n;
Demo d;
cout << "Addition of " << m << " and " << n << "is:" <<
d.Add(m, n) << endl;
cout << "Substraction of " << m << " and " << n <<
"is:" << d.Sub(m, n) << endl;
cout << "Multiplication of " << m << " and " << n <<
"is:" << d.Mul(m, n) << endl;
cout << "Division of " << m << " and " << n << "is:" <<
d.Div(m, n) << endl;
cout << "Average of " << m << " and " << n << "is:" <<
d.Avg(m, n) << endl;
return 0;
}
Output-:Enter to Number to perform Arithmetic
Operation:
93
Addition of 9 and 3is:12
Substraction of 9 and 3is:6
Multiplication of 9 and 3is:27
Division of 9 and 3is:3
Average of 9 and 3is:6
4. #include <iostream>
#include <string.h>
using namespace std;
char course[] = "BCA";
char gender[] = "MALE";
class student
{
public:
int roll;

char name[50];
int fee;
float per;
void Input_info()
{
cout << "Enter the Roll
Number,Name,fee ,percentage of student:\n";
cin >> roll;
fflush(stdin);
cin >> name;
fflush(stdin);
cin >> fee;
fflush(stdin);
cin >> per;
fflush(stdin);
}
void Display()
{
cout << "Student Name:" << name << endl;
cout << "Roll:" << roll << endl;
cout << "Course:" << course << endl;
cout << "Gender:" << gender << endl;
cout << "Fee:" << fee << endl;
cout << "Percenage:" << per << endl;
}
};
int main()
{
student s[5];
int roll;
for (int i = 0; i < 5; i++)
{
cout << "Enter the Information Of " << (i + 1) << "
student:\n";
s[i].Input_info();
}
while (true)
{
cout << "Enter roll Number of student you wants
to see Information -->for Exit press 6 :\n";
cin >> roll;
cout << "Information of student\n:";
switch (roll)
{
case 1:
s[0].Display();
break;
case 2:
s[1].Display();
break;
case 3:
s[2].Display();
break;
case 4:
s[3].Display();
break;
case 5:
s[4].Display();
break;
case 6:
exit(1);
break;
default:
cout << "Only Five student info is present .so
Enter 1 to 5:\n";
break;
}
}
}
Output-:
Enter the Information Of 1 student:
Enter the Roll Number,Name,fee ,percentage of
student:
1
nehal
125000
72.45
Enter the Information Of 2 student:
Enter the Roll Number,Name,fee ,percentage of
student:
2
kohli
141000
78
Enter the Information Of 3 student:
Enter the Roll Number,Name,fee ,percentage of
student:
3
rohit
152000
76
Enter the Information Of 4 student:
Enter the Roll Number,Name,fee ,percentage of
student:
4
Ishan
167000
67.7
Enter the Information Of 5 student:
Enter the Roll Number,Name,fee ,percentage of
student:
5
Siraj
178000
56.8
Enter roll Number of student you wants to see
Information -->for Exit press 6 :
3
Information of student
:Student Name:rohit
Roll:3
Course:BCA
Gender:MALE
Fee:152000
Percenage:76
Enter roll Number of student you wants to see
Information -->for Exit press 6 :
5. #include<iostream>
using namespace std;
class MyClass2;
class MyClass1
{
int a,b;
public:
void set_data()
{
cout<<"Enter a and b:\n";
cin>>a>>b;
}
friend void swap(MyClass1,MyClass2);
};
class MyClass2{
int a,b;
public:
void set_data()
{
cout<<"Enter a and b:\n";
cin>>a>>b;
}
friend void swap(MyClass1,MyClass2);
};
void swap(MyClass1 o1,MyClass2 o2)
{
MyClass1 temp;
MyClass2 temp1;
temp.a=o1.a;
o1.a=o2.a;
o2.a=temp.a;
temp1.b=o2.b;

o1.b=o2.b;
o2.b=temp1.b;
cout<<"\n for Class Myclass1:"<<"a="<<o1.a<<"
"<<"b="<<o1.b<<endl;
cout<<"\n for Class MyClass2:"<<"a="<<o2.a<<"
"<<"b="<<o2.b<<endl;

}
int main()
{
MyClass1 obj1;
MyClass2 obj2;
obj1.set_data();
obj2.set_data();
swap(obj1,obj2);

}
Output-:Enter a and b for MyClass1:
36
Enter a and b for MyClass2:
10 14

for Class Myclass1:a=10 b=14

for Class MyClass2:a=3 b=6


6. #include <iostream>
using namespace std;
class Demo
{
public:
void Addition(int x, int y)
{
cout << "Addition of " << x << " and " << y << " is :"
<< x + y << endl;
}
void Addition(int x, int y, int z)
{
cout << "Addition of " << x << "," << y << " and " <<
z << " is :" << x + y + z << endl;
}
void Addition(double x, double y)
{
cout << "Addition of " << x << " and " << y << " is :"
<< x + y << endl;
}
void Addition(double x, double y, double z)
{
cout << "Addition of " << x << "," << y << " and " <<
z << " is :" << x + y + z << endl;
}
void Addition(int x, int y, double z)
{
cout << "Addition of " << x << "," << y << " and " <<
z << " is :" << x + y + z << endl;
}
};
int main()
{
Demo d;
d.Addition(2, 5);
d.Addition(2, 5, 10);
d.Addition(2.3, 5.9);
d.Addition(2.12, 5.45, 9.12);
d.Addition(34, 56, 98.4);
}
Output-:Addition of 2 and 5 is :7
Addition of 2,5 and 10 is :17
Addition of 2.3 and 5.9 is :8.2
Addition of 2.12,5.45 and 9.12 is :16.69
Addition of 34,56 and 98.4 is :188.4
7. #include <iostream>
using namespace std;
class Demo
{
public:
int static count;
Demo()
{
count++;
}
GetCount()
{
return count;
}
};
int Demo::count = 0;
int main()
{
Demo d1, d2, d4, d6, d8;
cout << "Total Number of Object:" << d1.GetCount()
<< endl;
}
Output-:Total Number of Object:5
8. #include <iostream>
#include <string.h>
using namespace std;
char course[] = "BCA";
char gender[] = "MALE";
class student
{
public:
int roll;
char name[50];
float per;
int fee;
student()
{
}
student(int r, char n[], int f, float p)
{
roll = r;
strcpy(name, n);
fee = f;
per = p;
}
void show_Details();
};
void student::show_Details()
{

cout << "Name:" << name << endl;


cout << "Roll:" << roll << endl;
cout << "Course:" << course << endl;
cout << "Gender:" << gender << endl;
cout << "Fee:" << fee << endl;
cout << "percentage:" << per << endl;
}

int main()
{
student s[5] = {student(1, "Nehal kumar", 141500,
89), student(2, "Sachin Tendulakar", 15622, 78),
student(3, "KL Rahul", 234586, 86), student(4, "KL
Rahul", 234586, 86), student(5, "KL Rahul", 234586,
86)};

int r;
cout << "Enter roll number you wants to see
information =>press 6 for exit:\n";
cin >> r;
switch (r)
{
case 1:
s[0].show_Details();
break;
case 2:
s[1].show_Details();
break;
case 3:
s[2].show_Details();
break;
case 4:
s[3].show_Details();
break;
case 5:
s[4].show_Details();
break;
case 6:
exit(1);
break;
default:
cout << "Enter the valid Roll number";
break;
}
}
Output-:^
Enter roll number you wants to see information
=>press 6 for exit:
3
Name:KL Rahul
Roll:3
Course:BCA
Gender:MALE
Fee:234586
percentage:86
9. #include <iostream>
#include <math.h>
using namespace std;
class ArithmeticOperator
{
public:
int a, b;
void Addition(int a, int b)
{
cout << "Addition of " << a << " and " << b << " is:"
<< a + b << endl;
}
void Substraction(int a, int b)
{
cout << "Substraction of " << a << " and " << b << "
is " << a - b << endl;
}
void Multiplication(int a, int b)
{
cout << "Multiplication of " << a << " and " << b << "
is " << a * b << endl;
}
void Division(int a, int b)
{
cout << "Division " << a << " and " << b << " is " <<
a / b << endl;
}
virtual void AdditionOfSquares(int a, int b)
{
}
virtual void Power(int a, int b)
{
}
};
class addedFunction : public ArithmeticOperator
{
int a, b;
public:
void AdditionOfSquares(int a, int b)
{
cout << "Addition of squares of " << a << " and " <<
b << " is " << (a * a) + (b * b) << endl;
}
void Power(int a, int b)
{
cout << "Power " << pow(a, b) << endl;
}
};
int main()
{
ArithmeticOperator *ptr1;
ptr1->Addition(4, 9);
ptr1->Substraction(9, 3);
ptr1->Multiplication(3, 8);
ptr1->Division(21, 7);
addedFunction obj1;
ptr1 = &obj1;
ptr1->AdditionOfSquares(3, 4);
ptr1->Power(2, 3);
return 0;
}
Output-:Addition of 4 and 9 is:13
Substraction of 9 and 3 is 6
Multiplication of 3 and 8 is 24
Division 21 and 7 is 3
Addition of squares of 3 and 4 is 25
Power 8
10. //Write a class with 4 integer variables and 2
member functions to add 4 numbers and to calculate
average of 4 numbers
//Write a class with 4 integer variables and 2 member
functions to add 4 numbers and to calculate average
of 4 numbers
#include<iostream>
using namespace std;
class Demo
{
public:
int a,b,c,d;

Demo(int x,int y,int z,int u)


{
a=x;b=y;c=z;d=u;
}
void Add()
{
cout<<"Addition of four
Number:"<<a+b+c+d<<endl;
}
void Average()
{
cout<<"Average of number
is:"<<(a+b+c+d)/4.0<<endl;
}
};
int main()
{
Demo
d[5]={Demo(1,2,3,4),Demo(4,5,6,7),Demo(5,6,7,8),De
mo(1,2,3,4),Demo(10 ,23,25,56)};
for(int i=0;i<5;i++)
{
d[i].Add();
d[i].Average();
}
}Output-:Addition of four Number:10
Average of number is:2.5
Addition of four Number:22
Average of number is:5.5
Addition of four Number:26
Average of number is:6.5
Addition of four Number:10
Average of number is:2.5
Addition of four Number:114
Average of number is:28.5
11. //. Write a C++ generic function to add two
variables.
#include <iostream>
using namespace std;
template <class X, class Y>
X sum(X a, Y b)
{
return (a + b);
}
int main()
{
cout << "Sum of " << sum(34, 67.7) << endl;
cout << "Sum of " << sum(98, 61.7) << endl;
}
Output-:Sum of 101
Sum of 159
Sum Of Five Variable:344
12. //Write a C++ overloaded generic function to add
two variables and five variables
#include<iostream>
using namespace std;
template <class X,class Y,class Z,class U,class V>
X sum(X a,Y b,Z c,U d,V e)
{
return (a+b+c+d+e);

}
template <class M ,class N>
M sum(M a,N b)
{
return (a+b);
}
int main()
{
cout<<"Sum Of Two
Variable:"<<sum(23.9,78.98)<<endl;
cout<<"Sum Of Five
Variable:"<<sum(23,56.89,77,98,89.66)<<endl;
}
Output-:Sum Of Two Variable:102.88
Sum Of Five Variable:344
13. #include <iostream>
using namespace std;
class Shape
{
public:
double a, b;
void get_data()
{
cin >> a >> b;
}
void get_one_data()
{
cin >> a;
}
virtual void display_area(){};
};

class Triangle : public Shape


{
public:
void display_area()
{
cout << "Area of triangle " << 0.5 * a * b << endl;
}
};

class Rectangle : public Shape


{
public:
void display_area()
{
cout << "Area of rectangle " << a * b << endl;
}
};
class Square : public Shape
{
public:
void display_area()
{
cout << "Area of square " << a * a << endl;
}
};
int main()
{
Triangle t;
Shape *st = &t;
cout << "Enter base and altitude: ";
st->get_data();
st->display_area();

Rectangle r;
Shape *sr = &r;
cout << "Enter length and breadth: ";
sr->get_data();
sr->display_area();
Square s;
Shape *sq = &s;
cout << "Enter the side of square:\n";
sq->get_one_data();
sq->display_area();
return 0;
}

Output-:Same output of 14 number program…..


14. #include <iostream>
using namespace std;
class Shape
{
public:
double a, b;
void get_data()
{
cin >> a >> b;
}
void get_one_data()
{
cin >> a;
}
virtual void display_area() = 0;
};

class Triangle : public Shape


{
public:
void display_area()
{
cout << "Area of triangle " << 0.5 * a * b << endl;
}
};

class Rectangle : public Shape


{
public:
void display_area()
{
cout << "Area of rectangle " << a * b << endl;
}
};
class Square : public Shape
{
public:
void display_area()
{
cout << "Area of square " << a * a << endl;
}
};
int main()
{
Triangle t;
Shape *st = &t;
cout << "Enter base and altitude: ";
st->get_data();
st->display_area();

Rectangle r;
Shape *sr = &r;
cout << "Enter length and breadth: ";
sr->get_data();
sr->display_area();
Square s;
Shape *sq = &s;
cout << "Enter the side of square:\n";
sq->get_one_data();
sq->display_area();
return 0;
}

Output-:Enter base and altitude: 5 4


Area of triangle 10
Enter length and breadth: 5 4
Area of rectangle 20
Enter the side of square:
5
Area of square 25
15. Write a C++ program to get character inputs till user presses enter key. And display number of
characters with all the entered characters.(Use get() and put() functions)
//Write a C++ program to get character inputs till user presses enter key. And
display number of characters with
// all the entered characters.(Use get() and put() functions)
#include<iostream>
#include<stdio.h>
using namespace std;
char c[400];
void getWord()
{
int i=0;
while((c[i]=getchar())!='\n')
{
i++;
}
}

int main(void)
{
cout<<"Enter the string tills pres Enter:\n";
getWord();
cout<<"Your Enter String is:\n";
puts(c);
return 0;
}

Output-:
Enter the string tills pres Enter:
My name Is Nehal Kumar Lal . I pursing BCA course In
2021.
Your Enter String is:
My name Is Nehal Kumar Lal . I pursing BCA course In
2021.
16. // Reading Strings With getline()
#include <iostream>
using namespace std;
int main()
{
int size = 20;
char city[20];
cout << "city name:\n";
cin.getline(city, size);
cout << "Your city name is :" << city << endl;
cout << "enter city name again: \n";
cin.getline(city, size);

cout << "Your another city name : \n";


cout << city << endl;
return 0;
}

Output-:city name:
Darbhanga
Your city name is :Darbhanga
enter city name again:
Noida
Your another city name :
Noida
17. #include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "Enter three Numbers\n";
cin >> a >> b >> c;
int res = a - b;
// Some code
cout << "Before try \n";
try
{
cout << "Inside try \n";
if (res < 0)
{
throw res;
cout << "After throw (Never executed) \n";
}
else
{
cout << "Result will be:" << c / res << endl;
}
}
catch (int x)
{
cout << "Exception Caught \n";
cout << "This operation cannnot be completed\
n";
}
return 0;
}
Output-:1.
Enter three Numbers
23 34 10
Before try
Inside try
Exception Caught
This operation cannnot be completed
Output-2. Enter three Numbers
12 7 40
Before try
Inside try
Result will be:8
18. #include <iostream>
using namespace std;
int fun(int x, int c)
{
if (x < 0)
{
throw x;
cout << "After throw (Never executed) \n";
}
else
{
cout << "Result will be:" << c / x << endl;
}
}
int main()
{
int a, b, c;
cout << "Enter three Numbers\n";
cin >> a >> b >> c;

int res = a - b;
// Some code
cout << "Before try \n";
try
{
cout << "Inside try \n";
fun(res, c);
}
catch (int x)
{
cout << "Exception Caught \n";
cout<<"This Operation Cannot be completed\n";
}

cout << "After catch (Will be executed) \n";


return 0;
}
Output--: Output-:1.
Enter three Numbers
23 34 10
Before try
Inside try
Exception Caught
This operation cannnot be completed
Output-2. Enter three Numbers
12 7 40
Before try
Inside try
Result will be:8
19. /*Write a C++ program to take 1 integer, 1 double
and 1 character.
If integer given by user is greater than 1 then throw
integer value.
If integer given by user is smaller than 1 then throw
character value.
If integer given by user is equal to 1 then throw
double value.
(Exception handling with multiple catch blocks)*/
#include <iostream>
using namespace std;
int main()
{
int x;
float y;
char ch;
double z;

cout << "Enter the Single Value of integers types:\


n";
cin >> x;
cout << "Enter the single value of character types\
n";
cin >> ch;
cout << "Enter the single value of double types:\n";
cin >> z;
try
{
if (x > 1)
{
throw x;
}
else if (x < 1)
{
throw ch;
}
else
{
throw z;
}
}
catch (int e)
{
cout << "Expception caught: integer value:" << e
<< endl;
}
catch (char e3)
{
cout << "Expception caught: character value:" <<
e3 << endl;
}
catch (double e1)
{
cout << "Expception caught: double value:" << e1
<< endl;
}
}
Output-:a.
Enter the Single Value of integers types:
23
Enter the single value of character types
N
Enter the single value of double types:
45.45643
Expception caught: integer value:23
Output -b. Enter the Single Value of integers types:
-34
Enter the single value of character types
N
Enter the single value of double types:
23.7839293
Expception caught: character value:N
Output-c-: Enter the Single Value of integers types:
1
Enter the single value of character types
N
Enter the single value of double types:
343.5454223
Expception caught: double value:343.545
20. #include <iostream>
using namespace std;
/*Write a C++ program to take 3 integers a, b and c as
input and display output as follows:
Output: c/(a-b) if (a-b)>=1 and Message: “This
Operation cannot be completed” otherwise.
Use Exception handling*/
void input(int &a,int &b,int &c,int &res)
{
// int a, b, c;
cout << "Enter three Numbers\n";
cin >> a >> b >> c;

res = a - b;

}
int main()
{
int a,b ,c,res;
input(a,b,c,res);
// Some code
cout << "Before try \n";
try
{
cout << "Inside try \n";
if (res < 0)
{
throw res;
cout << "After throw (Never executed) \n";
}
else
{
cout << "Result will be:" << c / res << endl;
}
}
catch (int x)
{
cout << "Exception Caught \n";
cout << "Again Enter the correct data \n";
}
catch(float e)
{
cout<<"Exception Re-thorwn!";
}
input(a,b,c,res);
throw res;
return 0;
}
Output-: Enter three Numbers
12 20 14
Before try
Inside try
Exception Caught
Again Enter the correct data
Enter three Numbers
20 31 19
terminate called after throwing an instance of 'int'
21. #include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream file;
file.open ("Codespeedy.txt");
if (file.is_open())
{
file << "hello i am Raj gaurav.\n";
file << "hello i am Raj gaurav2.\n";
file.close();
}
else cout << "can not open a file";
return 0;
}
Output=: Enter the 3 number:
24 32 45
22. #include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int a,b,c,res;
ofstream file;
file.open ("Codespeedy.txt",ios::app);
cout<<"Enter the 3 number:\n";
cin>>a>>b>>c;
res=a+b+c;
if (file.is_open())
{
file << "Additio of Three number is:"<<res;
file.close();
}
else cout << "can not open a file";
return 0;
}

Output-Enter the 3 number:


234 233 2311

23. #include <iostream>


#include <fstream>
using namespace std;
int main ()
{
char ch;
ifstream fin;
fin.open ("Codespeedy.txt");
if (fin.is_open())
{
ch=fin.get();
while(!fin.eof())
{
cout<<ch;
ch=fin.get();
}
fin.close();
}
else cout << "can not open a file";
return 0;
}

OUTPUT-:::Additio of Three number is:2778

You might also like