C++ File Solution
C++ File Solution
C++ File Solution
#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
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;
}
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(){};
};
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;
}
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;
}
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);
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";
}
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;
}